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 # Add dedicated squaring procedure. Performance improvement varies
21 # from platform to platform, but in average it's ~5%/15%/25%/33%
22 # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
26 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
28 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
30 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
31 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
32 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
33 die "can't locate x86_64-xlate.pl";
35 open STDOUT,"| $^X $xlate $flavour $output";
38 $rp="%rdi"; # BN_ULONG *rp,
39 $ap="%rsi"; # const BN_ULONG *ap,
40 $bp="%rdx"; # const BN_ULONG *bp,
41 $np="%rcx"; # const BN_ULONG *np,
42 $n0="%r8"; # const BN_ULONG *n0,
43 $num="%r9"; # int num);
56 .type bn_mul_mont,\@function,6
80 lea (%rsp,%r10,8),%rsp # tp=alloca(8*(num+2))
81 and \$-1024,%rsp # minimize TLB usage
83 mov %r11,8(%rsp,$num,8) # tp[num+1]=%rsp
85 mov $bp,%r12 # reassign $bp
89 mov ($n0),$n0 # pull n0[0] value
94 mov ($bp),$m0 # m0=bp[0]
96 mulq $m0 # ap[0]*bp[0]
100 imulq $n0,%rax # "tp[0]"*n0
103 mulq ($np) # np[0]*m1
104 add $lo0,%rax # discarded
111 mulq $m0 # ap[j]*bp[0]
122 add $lo0,%rax # np[j]*m1+ap[j]*bp[0]
124 mov %rax,-16(%rsp,$j,8) # tp[j-1]
132 mov $hi1,-8(%rsp,$num,8)
133 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
140 mov ($bp,$i,8),$m0 # m0=bp[i]
141 mov ($ap),%rax # ap[0]
142 mulq $m0 # ap[0]*bp[i]
143 add (%rsp),%rax # ap[0]*bp[i]+tp[0]
148 imulq $n0,%rax # tp[0]*n0
151 mulq ($np,$j,8) # np[0]*m1
152 add $lo0,%rax # discarded
153 mov 8(%rsp),$lo0 # tp[1]
162 mulq $m0 # ap[j]*bp[i]
165 add %rax,$lo0 # ap[j]*bp[i]+tp[j]
174 add $lo0,%rax # np[j]*m1+ap[j]*bp[i]+tp[j]
178 mov %rax,-16(%rsp,$j,8) # tp[j-1]
185 add $lo0,$hi1 # pull upmost overflow bit
187 mov $hi1,-8(%rsp,$num,8)
188 mov %rdx,(%rsp,$num,8) # store upmost overflow bit
194 lea (%rsp),$ap # borrow ap for tp
195 lea -1($num),$j # j=num-1
197 mov ($ap),%rax # tp[0]
198 xor $i,$i # i=0 and clear CF!
201 .Lsub: sbb ($np,$i,8),%rax
202 mov %rax,($rp,$i,8) # rp[i]=tp[i]-np[i]
203 dec $j # doesn't affect CF!
204 mov 8($ap,$i,8),%rax # tp[i+1]
208 sbb \$0,%rax # handle upmost overflow bit
214 or $np,$ap # ap=borrow?tp:rp
216 .Lcopy: # copy or in-place refresh
218 mov %rax,($rp,$j,8) # rp[i]=tp[i]
219 mov $i,(%rsp,$j,8) # zap temporary vector
223 mov 8(%rsp,$num,8),%rsi # restore %rsp
234 .size bn_mul_mont,.-bn_mul_mont
237 ######################################################################
239 my $rptr="%rdi"; # const BN_ULONG *rptr,
240 my $aptr="%rsi"; # const BN_ULONG *aptr,
241 my $bptr="%rdx"; # not used
242 my $nptr="%rcx"; # const BN_ULONG *nptr,
243 my $n0 ="%r8"; # const BN_ULONG *n0);
244 my $num ="%r9"; # int num, has to be even and not less than 4
246 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
247 my @A0=("%r10","%r11");
248 my @A1=("%r12","%r13");
249 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
252 .type bn_sqr_mont,\@function,5
263 shl \$3,${num}d # convert $num to bytes
265 mov %rsp,%r11 # put aside %rsp
266 sub $num,%r10 # -$num
268 lea -72(%rsp,%r10,2),%rsp # alloca(frame+2*$num)
269 and \$-1024,%rsp # minimize TLB usage
270 ##############################################################
273 # +0 saved $num, used in reduction section
274 # +8 &t[2*$num], used in reduction section
281 mov $rptr,32(%rsp) # save $rptr
284 mov %r11, 56(%rsp) # save original %rsp
286 ##############################################################
289 # a) multiply-n-add everything but a[i]*a[i];
290 # b) shift result of a) by 1 to the left and accumulate
291 # a[i]*a[i] products;
293 lea 16(%r10),$i # $i=-($num-16)
294 lea ($aptr,$num),$aptr # end of a[] buffer, ($aptr,$i)=&ap[2]
296 mov $num,$j # $j=$num
299 .Lbzero: # clear t[$num]
307 .Lsqr_outer: # comments apply to $num==4 case
308 mov -16($aptr,$i),$a0 # a[0]
309 lea 64(%rsp,$num,2),$tptr # end of tp[] buffer, &tp[2*$num]
310 mov -8($aptr,$i),%rax # a[1]
311 lea -16($tptr,$i),$tptr # end of tp[] window, &tp[2*$num-"$i"]
312 mov ($aptr,$i),$ai # a[2]
315 mov -8($tptr,$i),$A0[0] # t[1]
318 add %rax,$A0[0] # a[1]*a[0]+t[1]
321 mov $A0[0],-8($tptr,$i) # t[1]
324 add ($tptr,$i),$A0[1] # a[2]*a[0]+t[2]
330 mov $A0[1],($tptr,$i) # t[2]
338 mov 8($aptr,$j),$ai # a[3]
340 add 8($tptr,$j),$A1[0]
343 add %rax,$A1[0] # a[2]*a[1]+t[3]
351 add %rax,$A0[0] # a[3]*a[0]+a[2]*a[1]+t[3]
354 mov $A0[0],8($tptr,$j) # t[3]
359 mov ($aptr,$j),$ai # a[4]
361 add ($tptr,$j),$A1[1]
364 add %rax,$A1[1] # a[3]*a[1]+t[4]
372 add %rax,$A0[1] # a[4]*a[0]+a[3]*a[1]+t[4]
375 mov $A0[1],($tptr,$j) # t[4]
385 mov -16($aptr),%rax # a[2]
388 mov $A1[1],($tptr) # t[4]
389 mov $A1[0],8($tptr) # t[5]
397 my ($shift,$carry)=($a0,$a1);
401 sub $num,$i # $i=8-$num
404 add $A1[0],%rax # t[5]
406 mov %rax,8($tptr) # t[5]
407 mov %rdx,16($tptr) # t[6]
408 mov $carry,24($tptr) # t[7]
410 mov -8($aptr,$i),%rax # a[0]
411 lea 64(%rsp,$num,2),$tptr
412 mov -16($tptr,$i,2),$A0[0] # t[0]
413 mov -8($tptr,$i,2),$A0[1] # t[1]
414 jmp .Lsqr_shift_n_add
418 lea ($shift,$A0[0],2),$A1[0]# t[2*i]<<1 | shift
420 lea (,$A0[1],2),$A1[1] # t[2*i+1]<<1 |
422 or $A0[0],$A1[1] # | t[2*i]>>63
423 mov 0($tptr,$i,2),$A0[0] # t[2*i+2] # prefetch
424 mov $A0[1],$shift # shift=t[2*i+1]>>63
426 mov 8($tptr,$i,2),$A0[1] # t[2*i+2+1] # prefetch
427 neg $carry # mov $carry,cf
429 mov 0($aptr,$i),%rax # a[i+1] # prefetch
431 mov $A1[0],-16($tptr,$i,2)
432 sbb $carry,$carry # mov cf,$carry
433 mov $A1[1],-8($tptr,$i,2)
435 jnz .Lsqr_shift_n_add
437 lea ($shift,$A0[0],2),$A1[0]# t[2*i]<<1|shift
439 lea (,$A0[1],2),$A1[1] # t[2*i+1]<<1 |
441 or $A0[0],$A1[1] # | t[2*i]>>63
443 neg $carry # mov $carry,cf
446 mov $A1[0],-16($tptr)
450 ##############################################################
451 # Montgomery reduction part, "word-by-word" algorithm.
454 my ($topbit,$nptr)=("%rbp",$aptr);
455 my ($m0,$m1)=($a0,$a1);
456 my @Ni=("%rbx","%r9");
458 mov 40(%rsp),$nptr # restore $nptr
460 mov $num,0(%rsp) # save $num
461 sub $num,$j # $j=-$num
462 mov 64(%rsp),$A0[0] # t[0] # modsched #
463 mov $n0,$m0 # # modsched #
464 lea 64(%rsp,$num,2),%rax # end of t[] buffer
465 lea 64(%rsp,$num),$tptr # end of t[] window
466 mov %rax,8(%rsp) # save end of t[] buffer
467 lea ($nptr,$num),$nptr # end of n[] buffer
468 xor $topbit,$topbit # $topbit=0
470 mov 0($nptr,$j),%rax # n[0] # modsched #
471 mov 8($nptr,$j),$Ni[1] # n[1] # modsched #
472 imulq $A0[0],$m0 # m0=t[0]*n0 # modsched #
473 mov %rax,$Ni[0] # # modsched #
480 add %rax,$A0[0] # n[0]*m0+t[0]
486 add 8($tptr,$j),$A0[1]
489 add %rax,$A0[1] # n[1]*m0+t[1]
499 mov ($nptr,$j),$Ni[0] # n[2]
504 add %rax,$A1[0] # n[0]*m1+"t[1]"
507 mov $A1[0],-8($tptr,$j) # "t[1]"
510 add ($tptr,$j),$A0[0]
513 add %rax,$A0[0] # n[2]*m0+t[2]
517 mov 8($nptr,$j),$Ni[1] # n[3]
522 add %rax,$A1[1] # n[1]*m1+"t[2]"
525 mov $A1[1],($tptr,$j) # "t[2]"
528 add 8($tptr,$j),$A0[1]
532 add %rax,$A0[1] # n[3]*m0+t[3]
538 sub 0(%rsp),$j # $j=-$num # modsched #
539 mov $n0,$m0 # # modsched #
545 add %rax,$A1[0] # n[2]*m1+"t[3]"
548 mov $A1[0],-8($tptr) # "t[3]"
551 add ($tptr),$A0[0] # +t[4]
553 mov 0($nptr,$j),$Ni[0] # n[0] # modsched #
557 imulq 16($tptr,$j),$m0 # m0=t[0]*n0 # modsched #
559 mov 8($nptr,$j),$Ni[1] # n[1] # modsched #
561 mov 16($tptr,$j),$A0[0] # t[0] # modsched #
564 add %rax,$A1[1] # n[3]*m1+"t[4]"
565 mov $Ni[0],%rax # # modsched #
567 mov $A1[1],($tptr) # "t[4]"
570 add 8($tptr),$A1[0] # +t[5]
573 lea 16($tptr),$tptr # "t[$num]>>128"
575 mov $A1[0],-8($tptr) # "t[5]"
576 cmp 8(%rsp),$tptr # are we done?
579 mov 0(%rsp),$num # restore $num
580 mov $topbit,($tptr) # save $topbit
583 ##############################################################
584 # Post-condition, 2x unrolled copy from bn_mul_mont
587 my ($tptr,$nptr)=("%rbx",$aptr);
589 lea 64(%rsp,$num),$tptr # upper half of t[2*$num] holds result
591 mov 32(%rsp),$rptr # restore $rptr
592 mov 40(%rsp),$nptr # restore $nptr
593 lea -1($num),$j # j=num/2-1
595 mov ($tptr),%rax # tp[0]
596 xor $i,$i # i=0 and clear CF!
600 mov 8($tptr,$i,8),%rdx
601 sbb 0($nptr,$i,8),%rax
602 sbb 8($nptr,$i,8),%rdx
603 mov %rax,0($rptr,$i,8) # rp[i]=tp[i]-np[i]
604 mov %rdx,8($rptr,$i,8) # rp[i]=tp[i]-np[i]
605 mov 16($tptr,$i,8),%rax # tp[i+1]
607 dec $j # doesn't affect CF!
610 sbb \$0,%rax # handle upmost overflow bit
617 or $nptr,$tptr # tp=borrow?tp:rp
619 lea 64(%rsp,$num,8),$nptr
620 lea ($nptr,$num,8),$nptr
623 .Lsqr_copy: # copy or in-place refresh
624 movdqu ($tptr,$i),%xmm1
625 movdqa %xmm0,64(%rsp,$i) # zap lower half of temporary vector
626 movdqa %xmm0,($nptr,$i) # zap upper half of temporary vector
627 movdqu %xmm1,($rptr,$i)
634 mov 56(%rsp),%rsi # restore %rsp
645 .size bn_sqr_mont,.-bn_sqr_mont
649 .asciz "Montgomery Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
653 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
654 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
662 .extern __imp_RtlVirtualUnwind
663 .type mul_handler,\@abi-omnipotent
677 mov 120($context),%rax # pull context->Rax
678 mov 248($context),%rbx # pull context->Rip
680 lea .Lprologue(%rip),%r10
681 cmp %r10,%rbx # context->Rip<.Lprologue
684 mov 152($context),%rax # pull context->Rsp
686 lea .Lepilogue(%rip),%r10
687 cmp %r10,%rbx # context->Rip>=.Lepilogue
688 jae .Lcommon_seh_tail
690 mov 192($context),%r10 # pull $num
691 mov 8(%rax,%r10,8),%rax # pull saved stack pointer
700 mov %rbx,144($context) # restore context->Rbx
701 mov %rbp,160($context) # restore context->Rbp
702 mov %r12,216($context) # restore context->R12
703 mov %r13,224($context) # restore context->R13
704 mov %r14,232($context) # restore context->R14
705 mov %r15,240($context) # restore context->R15
707 jmp .Lcommon_seh_tail
708 .size mul_handler,.-mul_handler
710 .type sqr_handler,\@abi-omnipotent
724 mov 120($context),%rax # pull context->Rax
725 mov 248($context),%rbx # pull context->Rip
727 lea .Lsqr_body(%rip),%r10
728 cmp %r10,%rbx # context->Rip<.Lsqr_body
731 mov 152($context),%rax # pull context->Rsp
733 lea .Lsqr_epilogue(%rip),%r10
734 cmp %r10,%rbx # context->Rip>=.Lsqr_epilogue
735 jae .Lcommon_seh_tail
737 mov 56(%rax),%rax # pull saved stack pointer
746 mov %rbx,144($context) # restore context->Rbx
747 mov %rbp,160($context) # restore context->Rbp
748 mov %r12,216($context) # restore context->R12
749 mov %r13,224($context) # restore context->R13
750 mov %r14,232($context) # restore context->R14
751 mov %r15,240($context) # restore context->R15
756 mov %rax,152($context) # restore context->Rsp
757 mov %rsi,168($context) # restore context->Rsi
758 mov %rdi,176($context) # restore context->Rdi
760 mov 40($disp),%rdi # disp->ContextRecord
761 mov $context,%rsi # context
762 mov \$154,%ecx # sizeof(CONTEXT)
763 .long 0xa548f3fc # cld; rep movsq
766 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
767 mov 8(%rsi),%rdx # arg2, disp->ImageBase
768 mov 0(%rsi),%r8 # arg3, disp->ControlPc
769 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
770 mov 40(%rsi),%r10 # disp->ContextRecord
771 lea 56(%rsi),%r11 # &disp->HandlerData
772 lea 24(%rsi),%r12 # &disp->EstablisherFrame
773 mov %r10,32(%rsp) # arg5
774 mov %r11,40(%rsp) # arg6
775 mov %r12,48(%rsp) # arg7
776 mov %rcx,56(%rsp) # arg8, (NULL)
777 call *__imp_RtlVirtualUnwind(%rip)
779 mov \$1,%eax # ExceptionContinueSearch
791 .size sqr_handler,.-sqr_handler
795 .rva .LSEH_begin_bn_mul_mont
796 .rva .LSEH_end_bn_mul_mont
797 .rva .LSEH_info_bn_mul_mont
799 .rva .LSEH_begin_bn_sqr_mont
800 .rva .LSEH_end_bn_sqr_mont
801 .rva .LSEH_info_bn_sqr_mont
805 .LSEH_info_bn_mul_mont:
808 .LSEH_info_bn_sqr_mont: