2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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 # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
20 # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
21 # the time being... gcc 4.3 appeared to generate poor code, therefore
22 # the effort. And indeed, the module delivers 55%-90%(*) improvement
23 # on heaviest ECDSA verify and ECDH benchmarks for 163- and 571-bit
24 # key lengths on z990, 30%-55%(*) - on z10, and 70%-110%(*) - on z196.
25 # This is for 64-bit build. In 32-bit "highgprs" case improvement is
26 # even higher, for example on z990 it was measured 80%-150%. ECDSA
27 # sign is modest 9%-12% faster. Keep in mind that these coefficients
28 # are not ones for bn_GF2m_mul_2x2 itself, as not all CPU time is
31 # (*) gcc 4.1 was observed to deliver better results than gcc 4.3,
32 # so that improvement coefficients can vary from one specific
35 # $output is the last argument if it looks like a file (it has an extension)
36 # $flavour is the first argument if it doesn't look like a file
37 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
38 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
40 if ($flavour =~ /3[12]/) {
48 $output and open STDOUT,">$output";
50 $stdframe=16*$SIZE_T+4*8;
64 ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(6..11));
65 ($lo,$hi,$b)=map("%r$_",(3..5)); $a=$lo; $mask=$a8;
70 .type _mul_1x1,\@function
78 srag $lo,$a1,63 # broadcast 63rd bit
80 srag @i[0],$a2,63 # broadcast 62nd bit
82 srag @i[1],$a4,63 # broadcast 61st bit
90 stg @T[0],`$stdframe+0*8`($sp) # tab[0]=0
92 stg $a1,`$stdframe+1*8`($sp) # tab[1]=a1
94 stg $a2,`$stdframe+2*8`($sp) # tab[2]=a2
96 stg $a12,`$stdframe+3*8`($sp) # tab[3]=a1^a2
99 stg $a4,`$stdframe+4*8`($sp) # tab[4]=a4
101 stg $a1,`$stdframe+5*8`($sp) # tab[5]=a1^a4
103 stg $a2,`$stdframe+6*8`($sp) # tab[6]=a2^a4
105 stg $a12,`$stdframe+7*8`($sp) # tab[7]=a1^a2^a4
108 stg $a8,`$stdframe+8*8`($sp) # tab[8]=a8
110 stg $a1,`$stdframe+9*8`($sp) # tab[9]=a1^a8
112 stg $a2,`$stdframe+10*8`($sp) # tab[10]=a2^a8
114 stg $a12,`$stdframe+11*8`($sp) # tab[11]=a1^a2^a8
117 stg $a48,`$stdframe+12*8`($sp) # tab[12]=a4^a8
119 stg $a1,`$stdframe+13*8`($sp) # tab[13]=a1^a4^a8
121 stg $a2,`$stdframe+14*8`($sp) # tab[14]=a2^a4^a8
123 stg $a12,`$stdframe+15*8`($sp) # tab[15]=a1^a2^a4^a8
138 xg $lo,$stdframe(@i[0],$sp)
142 for($n=1;$n<14;$n++) {
144 lg @T[1],$stdframe(@i[1],$sp)
145 srlg @i[1],$b,`($n+2)*4`-3
146 sllg @T[0],@T[1],`$n*4`
148 srlg @T[1],@T[1],`64-$n*4`
152 push(@i,shift(@i)); push(@T,shift(@T));
155 lg @T[1],$stdframe(@i[1],$sp)
156 sllg @T[0],@T[1],`$n*4`
157 srlg @T[1],@T[1],`64-$n*4`
161 lg @T[0],$stdframe(@i[0],$sp)
162 sllg @T[1],@T[0],`($n+1)*4`
163 srlg @T[0],@T[0],`64-($n+1)*4`
168 .size _mul_1x1,.-_mul_1x1
170 .globl bn_GF2m_mul_2x2
171 .type bn_GF2m_mul_2x2,\@function
174 stm${g} %r3,%r15,3*$SIZE_T($sp)
176 lghi %r1,-$stdframe-128
178 la $sp,0(%r1,$sp) # alloca
179 st${g} %r0,0($sp) # back chain
182 my @r=map("%r$_",(6..9));
184 bras $ra,_mul_1x1 # a1·b1
187 lg $a,`$stdframe+128+4*$SIZE_T`($sp)
188 lg $b,`$stdframe+128+6*$SIZE_T`($sp)
189 bras $ra,_mul_1x1 # a0·b0
192 lg $a,`$stdframe+128+3*$SIZE_T`($sp)
193 lg $b,`$stdframe+128+5*$SIZE_T`($sp)
194 xg $a,`$stdframe+128+4*$SIZE_T`($sp)
195 xg $b,`$stdframe+128+6*$SIZE_T`($sp)
196 bras $ra,_mul_1x1 # (a0+a1)·(b0+b1)
197 lmg @r[0],@r[3],0($rp)
222 lm${g} %r6,%r15,`$stdframe+128+6*$SIZE_T`($sp)
224 .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
225 .string "GF(2^m) Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
228 $code =~ s/\`([^\`]*)\`/eval($1)/gem;