3 # Copyright (c) 2010-2011 Intel Corp.
4 # Author: Vinodh.Gopal@intel.com
6 # Erdinc.Ozturk@intel.com
7 # Maxim.Perminov@intel.com
9 # More information about algorithm used can be found at:
10 # http://www.cse.buffalo.edu/srds2009/escs2009_submission_Gopal.pdf
12 # ====================================================================
13 # Copyright (c) 2011 The OpenSSL Project. All rights reserved.
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
19 # 1. Redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer.
22 # 2. Redistributions in binary form must reproduce the above copyright
23 # notice, this list of conditions and the following disclaimer in
24 # the documentation and/or other materials provided with the
27 # 3. All advertising materials mentioning features or use of this
28 # software must display the following acknowledgment:
29 # "This product includes software developed by the OpenSSL Project
30 # for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
32 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
33 # endorse or promote products derived from this software without
34 # prior written permission. For written permission, please contact
35 # licensing@OpenSSL.org.
37 # 5. Products derived from this software may not be called "OpenSSL"
38 # nor may "OpenSSL" appear in their names without prior written
39 # permission of the OpenSSL Project.
41 # 6. Redistributions of any form whatsoever must retain the following
43 # "This product includes software developed by the OpenSSL Project
44 # for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
46 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
47 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
50 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
57 # OF THE POSSIBILITY OF SUCH DAMAGE.
58 # ====================================================================
62 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
64 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
65 ( $xlate="${dir}x86_64-xlate.pl" and -f
$xlate ) or
66 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f
$xlate) or
67 die "can't locate x86_64-xlate.pl";
69 open OUT
,"| \"$^X\" $xlate $flavour $output";
80 #MULSTEP_512_ADD MACRO x7, x6, x5, x4, x3, x2, x1, x0, dst, src1, src2, add_src, tmp1, tmp2
82 # uses rax, rdx, and args
85 my ($x, $DST, $SRC2, $ASRC, $OP, $TMP)=@_;
86 my @X=@
$x; # make a copy
88 mov
(+8*0)($SRC2), %rax
89 mul
$OP # rdx:rax = %OP * [0]
95 for(my $i=1;$i<8;$i++) {
99 mov
(+8*$i)($SRC2), %rax
100 mul
$OP # rdx:rax = %OP * [$i]
101 mov
(+8*$i)($ASRC), $X[$i]
113 #MULSTEP_512 MACRO x7, x6, x5, x4, x3, x2, x1, x0, dst, src2, src1_val, tmp
115 # uses rax, rdx, and args
118 my ($x, $DST, $SRC2, $OP, $TMP)=@_;
119 my @X=@
$x; # make a copy
121 mov
(+8*0)($SRC2), %rax
122 mul
$OP # rdx:rax = %OP * [0]
127 for(my $i=1;$i<8;$i++) {
131 mov
(+8*$i)($SRC2), %rax
132 mul
$OP # rdx:rax = %OP * [$i]
148 # macro to copy data from flat space to swizzled table
149 #MACRO swizzle pDst, pSrc, tmp1, tmp2
150 # pDst and pSrc are modified
153 my ($pDst, $pSrc, $cnt, $d0)=@_;
160 mov
$d0#w, (+64*1)($pDst)
162 mov
$d0#w, (+64*2)($pDst)
164 mov
$d0#w, (+64*3)($pDst)
166 lea
64*4($pDst), $pDst
174 # macro to copy data from swizzled table to flat space
175 #MACRO unswizzle pDst, pSrc, tmp*3
178 my ($pDst, $pSrc, $cnt, $d0, $d1)=@_;
182 movzxw
(+64*3+256*0)($pSrc), $d0
183 movzxw
(+64*3+256*1)($pSrc), $d1
186 mov
(+64*2+256*0)($pSrc), $d0#w
187 mov
(+64*2+256*1)($pSrc), $d1#w
190 mov
(+64*1+256*0)($pSrc), $d0#w
191 mov
(+64*1+256*1)($pSrc), $d1#w
194 mov
(+64*0+256*0)($pSrc), $d0#w
195 mov
(+64*0+256*1)($pSrc), $d1#w
196 mov
$d0, (+8*0)($pDst)
197 mov
$d1, (+8*1)($pDst)
198 lea
256*2($pSrc), $pSrc
199 lea
8*2($pDst), $pDst
228 # 058 X1[11] P[9] Z[8]
229 # 050 X1[10] P[8] Z[7]
230 # 048 X1[9] P[7] Z[6]
231 # 040 X1[8] P[6] Z[5]
232 # 038 X1[7] P[5] Z[4]
233 # 030 X1[6] P[4] Z[3]
234 # 028 X1[5] P[3] Z[2]
235 # 020 X1[4] P[2] Z[1]
236 # 018 X1[3] P[1] Z[0]
237 # 010 X1[2] P[0] Y[2]
238 # 008 X1[1] Q[1] Y[1]
239 # 000 X1[0] Q[0] Y[0]
241 my $X1_offset = 0; # 13 qwords
242 my $X2_offset = $X1_offset + 13*8; # 11 qwords
243 my $Carries_offset = $X2_offset + 11*8; # 1 qword
244 my $Q_offset = 0; # 2 qwords
245 my $P_offset = $Q_offset + 2*8; # 11 qwords
246 my $Y_offset = 0; # 3 qwords
247 my $Z_offset = $Y_offset + 3*8; # 9 qwords
249 my $Red_Data_Size = $Carries_offset + 1*8; # (25 qwords)
256 # ... <old stack contents>
280 # 088 reduce result addr
293 # 008 pResult ; arg 1
294 # 000 rsp ; stack pointer before subtract
297 my $pResult_offset = 8*1 + $rsp_offset;
298 my $pG_offset = 8*1 + $pResult_offset;
299 my $pData_offset = 8*1 + $pG_offset;
300 my $i_offset = 8*1 + $pData_offset;
301 my $pg_offset = 8*1 + $i_offset;
302 my $loop_idx_offset = 8*1 + $pg_offset;
303 my $reserved1_offset = 8*1 + $loop_idx_offset;
304 my $exp_offset = 8*1 + $reserved1_offset;
305 my $red_result_addr_offset= 8*9 + $exp_offset;
306 my $reserved2_offset = 8*1 + $red_result_addr_offset;
307 my $Reduce_Data_offset = 8*5 + $reserved2_offset;
308 my $GT_offset = $Red_Data_Size + $Reduce_Data_offset;
309 my $tmp_offset = 8*8 + $GT_offset;
310 my $tmp16_offset = 8*8 + $tmp_offset;
311 my $garray_offset = 8*16 + $tmp16_offset;
312 my $mem_size = 8*8*32 + $garray_offset;
315 # Offsets within Reduce Data
318 # struct MODF_2FOLD_MONT_512_C1_DATA {
321 # UINT64 m1[8]; /* 2^768 % m */
322 # UINT64 m2[8]; /* 2^640 % m */
323 # UINT64 k1[2]; /* (- 1/m) % 2^128 */
327 my $M = 512; # = 8 * 8 * 8
328 my $M1 = 576; # = 8 * 8 * 9 /* += 8 * 8 */
329 my $M2 = 640; # = 8 * 8 * 10 /* += 8 * 8 */
330 my $K1 = 704; # = 8 * 8 * 11 /* += 8 * 8 */
338 # MULADD_128x512 : Function to multiply 128-bits (2 qwords) by 512-bits (8 qwords)
339 # and add 512-bits (8 qwords)
340 # to get 640 bits (10 qwords)
341 # Input: 128-bit mul source: [rdi+8*1], rbp
342 # 512-bit mul source: [rsi+8*n]
343 # 512-bit add source: r15, r14, ..., r9, r8
344 # Output: r9, r8, r15, r14, r13, r12, r11, r10, [rcx+8*1], [rcx+8*0]
345 # Clobbers all regs except: rcx, rsi, rdi
347 .type MULADD_128x512
,\
@abi-omnipotent
351 &MULSTEP_512
([map("%r$_",(8..15))], "(+8*0)(%rcx)", "%rsi", "%rbp", "%rbx");
353 mov
(+8*1)(%rdi), %rbp
355 &MULSTEP_512
([map("%r$_",(9..15,8))], "(+8*1)(%rcx)", "%rsi", "%rbp", "%rbx");
358 .size MULADD_128x512
,.-MULADD_128x512
363 #MULADD_256x512 MACRO pDst, pA, pB, OP, TMP, X7, X6, X5, X4, X3, X2, X1, X0
365 # Inputs: pDst: Destination (768 bits, 12 qwords)
366 # pA: Multiplicand (1024 bits, 16 qwords)
367 # pB: Multiplicand (512 bits, 8 qwords)
369 # where Ah is (in qwords) A[15:12] (256 bits) and Al is A[7:0] (512 bits)
370 # Results in X3 X2 X1 X0 X7 X6 X5 X4 Dst[3:0]
371 # Uses registers: arguments, RAX, RDX
374 my ($pDst, $pA, $pB, $OP, $TMP, $X)=@_;
376 mov
(+8*12)($pA), $OP
378 &MULSTEP_512_ADD
($X, "(+8*0)($pDst)", $pB, $pA, $OP, $TMP);
379 push(@
$X,shift(@
$X));
382 mov
(+8*13)($pA), $OP
384 &MULSTEP_512
($X, "(+8*1)($pDst)", $pB, $OP, $TMP);
385 push(@
$X,shift(@
$X));
388 mov
(+8*14)($pA), $OP
390 &MULSTEP_512
($X, "(+8*2)($pDst)", $pB, $OP, $TMP);
391 push(@
$X,shift(@
$X));
394 mov
(+8*15)($pA), $OP
396 &MULSTEP_512
($X, "(+8*3)($pDst)", $pB, $OP, $TMP);
397 push(@
$X,shift(@
$X));
401 # mont_reduce(UINT64 *x, /* 1024 bits, 16 qwords */
402 # UINT64 *m, /* 512 bits, 8 qwords */
403 # MODF_2FOLD_MONT_512_C1_DATA *data,
404 # UINT64 *r) /* 512 bits, 8 qwords */
405 # Input: x (number to be reduced): tmp16 (Implicit)
406 # m (modulus): [pM] (Implicit)
407 # data (reduce data): [pData] (Implicit)
408 # Output: r (result): Address in [red_res_addr]
409 # result also in: r9, r8, r15, r14, r13, r12, r11, r10
411 my @X=map("%r$_",(8..15));
414 .type mont_reduce
,\
@abi-omnipotent
423 lea
(+$Reduce_Data_offset+$X1_offset+$STACK_DEPTH)(%rsp), %rdi # pX1 (Dst) 769 bits, 13 qwords
424 mov
(+$pData_offset+$STACK_DEPTH)(%rsp), %rsi # pM1 (Bsrc) 512 bits, 8 qwords
426 lea
(+$tmp16_offset+$STACK_DEPTH)(%rsp), %rcx # X (Asrc) 1024 bits, 16 qwords
430 &MULADD_256x512
("%rdi", "%rcx", "%rsi", "%rbp", "%rbx", \
@X); # rotates @X 4 times
431 # results in r11, r10, r9, r8, r15, r14, r13, r12, X1[3:0]
436 add
(+8*8)(%rcx), $X[4]
437 adc
(+8*9)(%rcx), $X[5]
438 adc
(+8*10)(%rcx), $X[6]
439 adc
(+8*11)(%rcx), $X[7]
441 # X1 is now rax, r11-r8, r15-r12, tmp16[3:0]
444 # check for carry ;; carry stored in rax
445 mov
$X[4], (+8*8)(%rdi) # rdi points to X1
446 mov
$X[5], (+8*9)(%rdi)
448 mov
$X[7], (+8*11)(%rdi)
450 mov
%rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)
452 mov
(+8*0)(%rdi), $X[4]
453 mov
(+8*1)(%rdi), $X[5]
454 mov
(+8*2)(%rdi), $X[6]
455 mov
(+8*3)(%rdi), $X[7]
457 # X1 is now stored in: X1[11], rbp, X1[9:8], r15-r8
463 # do first part (X2 = Xh * M2)
464 add \
$8*10, %rdi # rdi -> pXh ; 128 bits, 2 qwords
465 # Xh is actually { [rdi+8*1], rbp }
466 add \
$`$M2-$M1`, %rsi # rsi -> M2
467 lea
(+$Reduce_Data_offset+$X2_offset+$STACK_DEPTH)(%rsp), %rcx # rcx -> pX2 ; 641 bits, 11 qwords
469 unshift(@X,pop(@X)); unshift(@X,pop(@X));
472 call MULADD_128x512
# args in rcx, rdi / rbp, rsi, r15-r8
473 # result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]
474 mov
(+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rax
477 add
(+8*8-8*10)(%rdi), $X[6] # (-8*10) is to adjust rdi -> Xh to Xl
478 adc
(+8*9-8*10)(%rdi), $X[7]
479 mov
$X[6], (+8*8)(%rcx)
480 mov
$X[7], (+8*9)(%rcx)
483 mov
%rax, (+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp)
485 lea
(+$Reduce_Data_offset+$Q_offset+$STACK_DEPTH)(%rsp), %rdi # rdi -> pQ ; 128 bits, 2 qwords
486 add \
$`$K1-$M2`, %rsi # rsi -> pK1 ; 128 bits, 2 qwords
488 # MUL_128x128t128 rdi, rcx, rsi ; Q = X2 * K1 (bottom half)
489 # B1:B0 = rsi[1:0] = K1[1:0]
490 # A1:A0 = rcx[1:0] = X2[1:0]
491 # Result = rdi[1],rbp = Q[1],rbp
493 mov
(+8*1)(%rsi), %rbx # B1
495 mov
(%rcx), %rax # A0
500 mov
(+8*1)(%rcx), %rax # A1
504 mov
(%rcx), %rax # A0
508 mov
%r9, (+8*1)(%rdi)
509 # end MUL_128x128t128
514 mov
(+8*1)(%rcx), $X[7] # r9:r8 = X2[1:0]
516 call MULADD_128x512
# args in rcx, rdi / rbp, rsi, r15-r8
517 # result in r9, r8, r15, r14, r13, r12, r11, r10, X2[1:0]
519 # load first half of m to rdx, rdi, rbx, rax
520 # moved this here for efficiency
521 mov
(+8*0)(%rsi), %rax
522 mov
(+8*1)(%rsi), %rbx
523 mov
(+8*2)(%rsi), %rdi
524 mov
(+8*3)(%rsi), %rdx
526 # continue with reduction
527 mov
(+$Reduce_Data_offset+$Carries_offset+$STACK_DEPTH)(%rsp), %rbp
529 add
(+8*8)(%rcx), $X[6]
530 adc
(+8*9)(%rcx), $X[7]
532 #accumulate the final carry to rbp
535 # Add in overflow corrections: R = (X2>>128) += T[overflow]
536 # R = {r9, r8, r15, r14, ..., r10}
538 mov
(+$pData_offset+$STACK_DEPTH)(%rsp), %rcx # rsi -> Data (and points to T)
539 add
%rcx, %rbp # pT ; 512 bits, 8 qwords, spread out
541 # rsi will be used to generate a mask after the addition
544 add
(+8*8*0)(%rbp), $X[0]
545 adc
(+8*8*1)(%rbp), $X[1]
546 adc
(+8*8*2)(%rbp), $X[2]
547 adc
(+8*8*3)(%rbp), $X[3]
548 adc
(+8*8*4)(%rbp), $X[4]
549 adc
(+8*8*5)(%rbp), $X[5]
550 adc
(+8*8*6)(%rbp), $X[6]
551 adc
(+8*8*7)(%rbp), $X[7]
553 # if there is a carry: rsi = 0xFFFFFFFFFFFFFFFF
554 # if carry is clear: rsi = 0x0000000000000000
557 # if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
569 # if there is a borrow: rbp = 0
570 # if there is no borrow: rbp = 1
571 # this is used to save the borrows in between the first half and the 2nd half of the subtraction of m
574 #load second half of m to rdx, rdi, rbx, rax
577 mov
(+8*4)(%rcx), %rax
578 mov
(+8*5)(%rcx), %rbx
579 mov
(+8*6)(%rcx), %rdi
580 mov
(+8*7)(%rcx), %rdx
582 # use the rsi mask as before
583 # if carry is clear, subtract 0. Otherwise, subtract 256 bits of m
589 # if rbp = 0, there was a borrow before, it is moved to the carry flag
590 # if rbp = 1, there was not a borrow before, carry flag is cleared
598 # write R back to memory
600 mov
(+$red_result_addr_offset+$STACK_DEPTH)(%rsp), %rsi
601 mov
$X[0], (+8*0)(%rsi)
602 mov
$X[1], (+8*1)(%rsi)
603 mov
$X[2], (+8*2)(%rsi)
604 mov
$X[3], (+8*3)(%rsi)
605 mov
$X[4], (+8*4)(%rsi)
606 mov
$X[5], (+8*5)(%rsi)
607 mov
$X[6], (+8*6)(%rsi)
608 mov
$X[7], (+8*7)(%rsi)
611 .size mont_reduce
,.-mont_reduce
616 #MUL_512x512 MACRO pDst, pA, pB, x7, x6, x5, x4, x3, x2, x1, x0, tmp*2
618 # Inputs: pDst: Destination (1024 bits, 16 qwords)
619 # pA: Multiplicand (512 bits, 8 qwords)
620 # pB: Multiplicand (512 bits, 8 qwords)
621 # Uses registers rax, rdx, args
622 # B operand in [pB] and also in x7...x0
625 my ($pDst, $pA, $pB, $x, $OP, $TMP, $pDst_o)=@_;
626 my ($pDst, $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
627 my @X=@
$x; # make a copy
633 mul
$OP # rdx:rax = %OP * [0]
634 mov
%rax, (+$pDst_o+8*0)($pDst)
637 for(my $i=1;$i<8;$i++) {
640 mul
$OP # rdx:rax = %OP * [$i]
647 for(my $i=1;$i<8;$i++) {
649 mov
(+8*$i)($pA), $OP
652 &MULSTEP_512
(\
@X, "(+$pDst_o+8*$i)($pDst)", $pB, $OP, $TMP);
657 mov
$X[0], (+$pDst_o+8*8)($pDst)
658 mov
$X[1], (+$pDst_o+8*9)($pDst)
659 mov
$X[2], (+$pDst_o+8*10)($pDst)
660 mov
$X[3], (+$pDst_o+8*11)($pDst)
661 mov
$X[4], (+$pDst_o+8*12)($pDst)
662 mov
$X[5], (+$pDst_o+8*13)($pDst)
663 mov
$X[6], (+$pDst_o+8*14)($pDst)
664 mov
$X[7], (+$pDst_o+8*15)($pDst)
669 # mont_mul_a3b : subroutine to compute (Src1 * Src2) % M (all 512-bits)
670 # Input: src1: Address of source 1: rdi
671 # src2: Address of source 2: rsi
672 # Output: dst: Address of destination: [red_res_addr]
673 # src2 and result also in: r9, r8, r15, r14, r13, r12, r11, r10
674 # Temp: Clobbers [tmp16], all registers
676 .type mont_mul_a3b
,\
@abi-omnipotent
680 # multiply tmp = src1 * src2
681 # For multiply: dst = rcx, src1 = rdi, src2 = rsi
682 # stack depth is extra 8 from call
684 &MUL_512x512
("%rsp+$tmp16_offset+8", "%rdi", "%rsi", [map("%r$_",(10..15,8..9))], "%rbp", "%rbx");
688 # Call reduce(tmp, m, data, dst)
690 # tail recursion optimization: jmp to mont_reduce and return from there
694 .size mont_mul_a3b
,.-mont_mul_a3b
699 #SQR_512 MACRO pDest, pA, x7, x6, x5, x4, x3, x2, x1, x0, tmp*4
701 # Input in memory [pA] and also in x7...x0
702 # Uses all argument registers plus rax and rdx
704 # This version computes all of the off-diagonal terms into memory,
705 # and then it adds in the diagonal terms
709 my ($pDst, $pA, $x, $A, $tmp, $x7, $x6, $pDst_o)=@_;
710 my ($pDst, $pDst_o) = ($pDst =~ m/([^+]*)\+?(.*)?/);
711 my @X=@
$x; # make a copy
720 mov
%rax, (+$pDst_o+8*1)($pDst)
722 for(my $i=2;$i<8;$i++) {
734 mov
$X[0], (+$pDst_o+8*2)($pDst)
737 # second pass 12...17
746 mov
$X[1], (+$pDst_o+8*3)($pDst)
755 mov
$X[2], (+$pDst_o+8*4)($pDst)
800 mov
$X[3], (+$pDst_o+8*5)($pDst)
809 mov
$X[4], (+$pDst_o+8*6)($pDst)
838 # fourth pass 34...37
847 mov
$X[5], (+$pDst_o+8*7)($pDst)
856 mov
$x7, (+$pDst_o+8*8)($pDst)
885 mov
$X[1], (+$pDst_o+8*9)($pDst)
894 mov
$X[2], (+$pDst_o+8*10)($pDst)
915 mov
$X[5], (+$pDst_o+8*11)($pDst)
924 mov
$X[1], (+$pDst_o+8*12)($pDst)
937 mov
$X[2], (+$pDst_o+8*13)($pDst)
939 mov
%rdx, (+$pDst_o+8*14)($pDst)
941 # start finalize (add in squares, and double off-terms)
942 mov
(+$pDst_o+8*1)($pDst), $X[0]
943 mov
(+$pDst_o+8*2)($pDst), $X[1]
944 mov
(+$pDst_o+8*3)($pDst), $X[2]
945 mov
(+$pDst_o+8*4)($pDst), $X[3]
946 mov
(+$pDst_o+8*5)($pDst), $X[4]
947 mov
(+$pDst_o+8*6)($pDst), $X[5]
949 mov
(+8*3)($pA), %rax
962 mov
(+8*0)($pA), %rax
964 mov
%rax, (+$pDst_o+8*0)($pDst)
967 mov
(+8*1)($pA), %rax
975 mov
$X[0], (+$pDst_o+8*1)($pDst)
976 mov
$X[1], (+$pDst_o+8*2)($pDst)
978 mov
(+8*2)($pA), %rax
987 mov
$X[2], (+$pDst_o+8*3)($pDst)
988 mov
$X[3], (+$pDst_o+8*4)($pDst)
995 mov
$X[4], (+$pDst_o+8*5)($pDst)
996 mov
$X[5], (+$pDst_o+8*6)($pDst)
998 # %%tmp has 0/1 in column 7
999 # %%A6 has a full value in column 7
1001 mov
(+$pDst_o+8*7)($pDst), $X[0]
1002 mov
(+$pDst_o+8*8)($pDst), $X[1]
1003 mov
(+$pDst_o+8*9)($pDst), $X[2]
1004 mov
(+$pDst_o+8*10)($pDst), $X[3]
1005 mov
(+$pDst_o+8*11)($pDst), $X[4]
1006 mov
(+$pDst_o+8*12)($pDst), $X[5]
1007 mov
(+$pDst_o+8*13)($pDst), $x6
1008 mov
(+$pDst_o+8*14)($pDst), $x7
1027 mov
(+8*4)($pA), %rax
1036 mov
$X[0], (+$pDst_o+8*7)($pDst)
1037 mov
$X[1], (+$pDst_o+8*8)($pDst)
1039 mov
(+8*5)($pA), %rax
1048 mov
$X[2], (+$pDst_o+8*9)($pDst)
1049 mov
$X[3], (+$pDst_o+8*10)($pDst)
1051 mov
(+8*6)($pA), %rax
1058 mov
$X[4], (+$pDst_o+8*11)($pDst)
1059 mov
$X[5], (+$pDst_o+8*12)($pDst)
1065 mov
$x6, (+$pDst_o+8*13)($pDst)
1066 mov
$x7, (+$pDst_o+8*14)($pDst)
1067 mov
$A, (+$pDst_o+8*15)($pDst)
1072 # sqr_reduce: subroutine to compute Result = reduce(Result * Result)
1074 # input and result also in: r9, r8, r15, r14, r13, r12, r11, r10
1077 .type sqr_reduce
,\
@abi-omnipotent
1080 mov
(+$pResult_offset+8)(%rsp), %rcx
1082 &SQR_512
("%rsp+$tmp16_offset+8", "%rcx", [map("%r$_",(10..15,8..9))], "%rbx", "%rbp", "%rsi", "%rdi");
1084 # tail recursion optimization: jmp to mont_reduce and return from there
1088 .size sqr_reduce
,.-sqr_reduce
1096 #mod_exp_512(UINT64 *result, /* 512 bits, 8 qwords */
1097 # UINT64 *g, /* 512 bits, 8 qwords */
1098 # UINT64 *exp, /* 512 bits, 8 qwords */
1099 # struct mod_ctx_512 *data)
1102 # table size = 2^5 = 32
1103 #table_entries equ 32
1104 #table_size equ table_entries * 8
1107 .type mod_exp_512
,\
@function,4
1116 # adjust stack down and then align it with cache boundary
1118 sub \
$$mem_size, %rsp
1121 # store previous stack pointer and arguments
1122 mov
%r8, (+$rsp_offset)(%rsp)
1123 mov
%rdi, (+$pResult_offset)(%rsp)
1124 mov
%rsi, (+$pG_offset)(%rsp)
1125 mov
%rcx, (+$pData_offset)(%rsp)
1127 # transform g into montgomery space
1128 # GT = reduce(g * C2) = reduce(g * (2^256))
1129 # reduce expects to have the input in [tmp16]
1131 movdqu
(+16*0)(%rsi), %xmm0
1132 movdqu
(+16*1)(%rsi), %xmm1
1133 movdqu
(+16*2)(%rsi), %xmm2
1134 movdqu
(+16*3)(%rsi), %xmm3
1135 movdqa
%xmm4, (+$tmp16_offset+16*0)(%rsp)
1136 movdqa
%xmm4, (+$tmp16_offset+16*1)(%rsp)
1137 movdqa
%xmm4, (+$tmp16_offset+16*6)(%rsp)
1138 movdqa
%xmm4, (+$tmp16_offset+16*7)(%rsp)
1139 movdqa
%xmm0, (+$tmp16_offset+16*2)(%rsp)
1140 movdqa
%xmm1, (+$tmp16_offset+16*3)(%rsp)
1141 movdqa
%xmm2, (+$tmp16_offset+16*4)(%rsp)
1142 movdqa
%xmm3, (+$tmp16_offset+16*5)(%rsp)
1144 # load pExp before rdx gets blown away
1145 movdqu
(+16*0)(%rdx), %xmm0
1146 movdqu
(+16*1)(%rdx), %xmm1
1147 movdqu
(+16*2)(%rdx), %xmm2
1148 movdqu
(+16*3)(%rdx), %xmm3
1150 lea
(+$GT_offset)(%rsp), %rbx
1151 mov
%rbx, (+$red_result_addr_offset)(%rsp)
1154 # Initialize tmp = C
1155 lea
(+$tmp_offset)(%rsp), %rcx
1157 mov
%rax, (+8*0)(%rcx)
1158 mov
%rax, (+8*1)(%rcx)
1159 mov
%rax, (+8*3)(%rcx)
1160 mov
%rax, (+8*4)(%rcx)
1161 mov
%rax, (+8*5)(%rcx)
1162 mov
%rax, (+8*6)(%rcx)
1163 mov
%rax, (+8*7)(%rcx)
1164 mov
%rax, (+$exp_offset+8*8)(%rsp)
1165 movq \
$1, (+8*2)(%rcx)
1167 lea
(+$garray_offset)(%rsp), %rbp
1168 mov
%rcx, %rsi # pTmp
1169 mov
%rbp, %rdi # Garray[][0]
1172 &swizzle
("%rdi", "%rcx", "%rax", "%rbx");
1174 # for (rax = 31; rax != 0; rax--) {
1175 # tmp = reduce(tmp * G)
1180 mov
%rax, (+$i_offset)(%rsp)
1181 mov
%rbp, (+$pg_offset)(%rsp)
1183 mov
%rsi, (+$red_result_addr_offset)(%rsp)
1184 mov
(+8*0)(%rsi), %r10
1185 mov
(+8*1)(%rsi), %r11
1186 mov
(+8*2)(%rsi), %r12
1187 mov
(+8*3)(%rsi), %r13
1188 mov
(+8*4)(%rsi), %r14
1189 mov
(+8*5)(%rsi), %r15
1190 mov
(+8*6)(%rsi), %r8
1191 mov
(+8*7)(%rsi), %r9
1193 lea
(+$GT_offset)(%rsp), %rdi
1195 lea
(+$tmp_offset)(%rsp), %rsi
1196 mov
(+$pg_offset)(%rsp), %rbp
1198 mov
%rbp, (+$pg_offset)(%rsp)
1199 mov
%rsi, %rcx # rcx = rsi = addr of tmp
1202 &swizzle
("%rbp", "%rcx", "%rax", "%rbx");
1204 mov
(+$i_offset)(%rsp), %rax
1206 mov
%rax, (+$i_offset)(%rsp)
1210 # Copy exponent onto stack
1211 movdqa
%xmm0, (+$exp_offset+16*0)(%rsp)
1212 movdqa
%xmm1, (+$exp_offset+16*1)(%rsp)
1213 movdqa
%xmm2, (+$exp_offset+16*2)(%rsp)
1214 movdqa
%xmm3, (+$exp_offset+16*3)(%rsp)
1219 # Initialize result to G[exp{511:507}]
1220 mov
(+$exp_offset+62)(%rsp), %eax
1224 mov
%edx, (+$exp_offset+62)(%rsp)
1225 lea
(+$garray_offset)(%rsp,%rax,2), %rsi
1226 mov
(+$pResult_offset)(%rsp), %rdx
1229 &unswizzle
("%rdx", "%rsi", "%rbp", "%rbx", "%rax");
1233 # rcx = [loop_idx] = index: 510-5 to 0 by 5
1235 movq \
$505, (+$loop_idx_offset)(%rsp)
1237 mov
(+$pResult_offset)(%rsp), %rcx
1238 mov
%rcx, (+$red_result_addr_offset)(%rsp)
1239 mov
(+8*0)(%rcx), %r10
1240 mov
(+8*1)(%rcx), %r11
1241 mov
(+8*2)(%rcx), %r12
1242 mov
(+8*3)(%rcx), %r13
1243 mov
(+8*4)(%rcx), %r14
1244 mov
(+8*5)(%rcx), %r15
1245 mov
(+8*6)(%rcx), %r8
1246 mov
(+8*7)(%rcx), %r9
1258 # Do multiply, first look up proper value in Garray
1259 mov
(+$loop_idx_offset)(%rsp), %rcx # bit index
1261 shr \
$4, %rax # rax is word pointer
1262 mov
(+$exp_offset)(%rsp,%rax,2), %edx
1267 lea
(+$garray_offset)(%rsp,%rdx,2), %rsi
1268 lea
(+$tmp_offset)(%rsp), %rdx
1272 &unswizzle
("%rdx", "%rsi", "%rbp", "%rbx", "%rax");
1276 # Call mod_mul_a1(pDst, pSrc1, pSrc2, pM, pData)
1277 # result result pG M Data
1279 mov
(+$pResult_offset)(%rsp), %rsi
1284 mov
(+$loop_idx_offset)(%rsp), %rcx
1286 mov
%rcx, (+$loop_idx_offset)(%rsp)
1292 # transform result out of Montgomery space
1293 # result = reduce(result)
1294 mov
(+$pResult_offset)(%rsp), %rdx
1296 movdqu
(+16*0)(%rdx), %xmm0
1297 movdqu
(+16*1)(%rdx), %xmm1
1298 movdqu
(+16*2)(%rdx), %xmm2
1299 movdqu
(+16*3)(%rdx), %xmm3
1300 movdqa
%xmm4, (+$tmp16_offset+16*4)(%rsp)
1301 movdqa
%xmm4, (+$tmp16_offset+16*5)(%rsp)
1302 movdqa
%xmm4, (+$tmp16_offset+16*6)(%rsp)
1303 movdqa
%xmm4, (+$tmp16_offset+16*7)(%rsp)
1304 movdqa
%xmm0, (+$tmp16_offset+16*0)(%rsp)
1305 movdqa
%xmm1, (+$tmp16_offset+16*1)(%rsp)
1306 movdqa
%xmm2, (+$tmp16_offset+16*2)(%rsp)
1307 movdqa
%xmm3, (+$tmp16_offset+16*3)(%rsp)
1310 # If result > m, subract m
1311 # load result into r15:r8
1312 mov
(+$pResult_offset)(%rsp), %rax
1313 mov
(+8*0)(%rax), %r8
1314 mov
(+8*1)(%rax), %r9
1315 mov
(+8*2)(%rax), %r10
1316 mov
(+8*3)(%rax), %r11
1317 mov
(+8*4)(%rax), %r12
1318 mov
(+8*5)(%rax), %r13
1319 mov
(+8*6)(%rax), %r14
1320 mov
(+8*7)(%rax), %r15
1323 mov
(+$pData_offset)(%rsp), %rbx
1326 sub (+8*0)(%rbx), %r8
1327 sbb
(+8*1)(%rbx), %r9
1328 sbb
(+8*2)(%rbx), %r10
1329 sbb
(+8*3)(%rbx), %r11
1330 sbb
(+8*4)(%rbx), %r12
1331 sbb
(+8*5)(%rbx), %r13
1332 sbb
(+8*6)(%rbx), %r14
1333 sbb
(+8*7)(%rbx), %r15
1335 # if Carry is clear, replace result with difference
1336 mov
(+8*0)(%rax), %rsi
1337 mov
(+8*1)(%rax), %rdi
1338 mov
(+8*2)(%rax), %rcx
1339 mov
(+8*3)(%rax), %rdx
1344 mov
%rsi, (+8*0)(%rax)
1345 mov
%rdi, (+8*1)(%rax)
1346 mov
%rcx, (+8*2)(%rax)
1347 mov
%rdx, (+8*3)(%rax)
1349 mov
(+8*4)(%rax), %rsi
1350 mov
(+8*5)(%rax), %rdi
1351 mov
(+8*6)(%rax), %rcx
1352 mov
(+8*7)(%rax), %rdx
1357 mov
%rsi, (+8*4)(%rax)
1358 mov
%rdi, (+8*5)(%rax)
1359 mov
%rcx, (+8*6)(%rax)
1360 mov
%rdx, (+8*7)(%rax)
1362 mov
(+$rsp_offset)(%rsp), %rsi
1372 .size mod_exp_512
, . - mod_exp_512
1377 if ($reg =~ /%r[0-9]+/) { $reg .= $conv; }
1378 elsif ($conv eq "b") { $reg =~ s/%[er]([^x]+)x?/%$1l/; }
1379 elsif ($conv eq "w") { $reg =~ s/%[er](.+)/%$1/; }
1380 elsif ($conv eq "d") { $reg =~ s/%[er](.+)/%e$1/; }
1384 $code =~ s/(%[a-z0-9]+)#([bwd])/reg_part($1,$2)/gem;
1385 $code =~ s/\`([^\`]*)\`/eval $1/gem;
1386 $code =~ s/(\(\+[^)]+\))/eval $1/gem;