1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ci# 10e1051a39Sopenharmony_ci# ==================================================================== 11e1051a39Sopenharmony_ci# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL 12e1051a39Sopenharmony_ci# project. The module is, however, dual licensed under OpenSSL and 13e1051a39Sopenharmony_ci# CRYPTOGAMS licenses depending on where you obtain it. For further 14e1051a39Sopenharmony_ci# details see http://www.openssl.org/~appro/cryptogams/. 15e1051a39Sopenharmony_ci# ==================================================================== 16e1051a39Sopenharmony_ci# 17e1051a39Sopenharmony_ci# May 2011 18e1051a39Sopenharmony_ci# 19e1051a39Sopenharmony_ci# The module implements bn_GF2m_mul_2x2 polynomial multiplication used 20e1051a39Sopenharmony_ci# in bn_gf2m.c. It's kind of low-hanging mechanical port from C for 21e1051a39Sopenharmony_ci# the time being... gcc 4.3 appeared to generate poor code, therefore 22e1051a39Sopenharmony_ci# the effort. And indeed, the module delivers 55%-90%(*) improvement 23e1051a39Sopenharmony_ci# on heaviest ECDSA verify and ECDH benchmarks for 163- and 571-bit 24e1051a39Sopenharmony_ci# key lengths on z990, 30%-55%(*) - on z10, and 70%-110%(*) - on z196. 25e1051a39Sopenharmony_ci# This is for 64-bit build. In 32-bit "highgprs" case improvement is 26e1051a39Sopenharmony_ci# even higher, for example on z990 it was measured 80%-150%. ECDSA 27e1051a39Sopenharmony_ci# sign is modest 9%-12% faster. Keep in mind that these coefficients 28e1051a39Sopenharmony_ci# are not ones for bn_GF2m_mul_2x2 itself, as not all CPU time is 29e1051a39Sopenharmony_ci# burnt in it... 30e1051a39Sopenharmony_ci# 31e1051a39Sopenharmony_ci# (*) gcc 4.1 was observed to deliver better results than gcc 4.3, 32e1051a39Sopenharmony_ci# so that improvement coefficients can vary from one specific 33e1051a39Sopenharmony_ci# setup to another. 34e1051a39Sopenharmony_ci 35e1051a39Sopenharmony_ci# $output is the last argument if it looks like a file (it has an extension) 36e1051a39Sopenharmony_ci# $flavour is the first argument if it doesn't look like a file 37e1051a39Sopenharmony_ci$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; 38e1051a39Sopenharmony_ci$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef; 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_ciif ($flavour =~ /3[12]/) { 41e1051a39Sopenharmony_ci $SIZE_T=4; 42e1051a39Sopenharmony_ci $g=""; 43e1051a39Sopenharmony_ci} else { 44e1051a39Sopenharmony_ci $SIZE_T=8; 45e1051a39Sopenharmony_ci $g="g"; 46e1051a39Sopenharmony_ci} 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci$output and open STDOUT,">$output"; 49e1051a39Sopenharmony_ci 50e1051a39Sopenharmony_ci$stdframe=16*$SIZE_T+4*8; 51e1051a39Sopenharmony_ci 52e1051a39Sopenharmony_ci$rp="%r2"; 53e1051a39Sopenharmony_ci$a1="%r3"; 54e1051a39Sopenharmony_ci$a0="%r4"; 55e1051a39Sopenharmony_ci$b1="%r5"; 56e1051a39Sopenharmony_ci$b0="%r6"; 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci$ra="%r14"; 59e1051a39Sopenharmony_ci$sp="%r15"; 60e1051a39Sopenharmony_ci 61e1051a39Sopenharmony_ci@T=("%r0","%r1"); 62e1051a39Sopenharmony_ci@i=("%r12","%r13"); 63e1051a39Sopenharmony_ci 64e1051a39Sopenharmony_ci($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(6..11)); 65e1051a39Sopenharmony_ci($lo,$hi,$b)=map("%r$_",(3..5)); $a=$lo; $mask=$a8; 66e1051a39Sopenharmony_ci 67e1051a39Sopenharmony_ci$code.=<<___; 68e1051a39Sopenharmony_ci.text 69e1051a39Sopenharmony_ci 70e1051a39Sopenharmony_ci.type _mul_1x1,\@function 71e1051a39Sopenharmony_ci.align 16 72e1051a39Sopenharmony_ci_mul_1x1: 73e1051a39Sopenharmony_ci lgr $a1,$a 74e1051a39Sopenharmony_ci sllg $a2,$a,1 75e1051a39Sopenharmony_ci sllg $a4,$a,2 76e1051a39Sopenharmony_ci sllg $a8,$a,3 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ci srag $lo,$a1,63 # broadcast 63rd bit 79e1051a39Sopenharmony_ci nihh $a1,0x1fff 80e1051a39Sopenharmony_ci srag @i[0],$a2,63 # broadcast 62nd bit 81e1051a39Sopenharmony_ci nihh $a2,0x3fff 82e1051a39Sopenharmony_ci srag @i[1],$a4,63 # broadcast 61st bit 83e1051a39Sopenharmony_ci nihh $a4,0x7fff 84e1051a39Sopenharmony_ci ngr $lo,$b 85e1051a39Sopenharmony_ci ngr @i[0],$b 86e1051a39Sopenharmony_ci ngr @i[1],$b 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ci lghi @T[0],0 89e1051a39Sopenharmony_ci lgr $a12,$a1 90e1051a39Sopenharmony_ci stg @T[0],`$stdframe+0*8`($sp) # tab[0]=0 91e1051a39Sopenharmony_ci xgr $a12,$a2 92e1051a39Sopenharmony_ci stg $a1,`$stdframe+1*8`($sp) # tab[1]=a1 93e1051a39Sopenharmony_ci lgr $a48,$a4 94e1051a39Sopenharmony_ci stg $a2,`$stdframe+2*8`($sp) # tab[2]=a2 95e1051a39Sopenharmony_ci xgr $a48,$a8 96e1051a39Sopenharmony_ci stg $a12,`$stdframe+3*8`($sp) # tab[3]=a1^a2 97e1051a39Sopenharmony_ci xgr $a1,$a4 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_ci stg $a4,`$stdframe+4*8`($sp) # tab[4]=a4 100e1051a39Sopenharmony_ci xgr $a2,$a4 101e1051a39Sopenharmony_ci stg $a1,`$stdframe+5*8`($sp) # tab[5]=a1^a4 102e1051a39Sopenharmony_ci xgr $a12,$a4 103e1051a39Sopenharmony_ci stg $a2,`$stdframe+6*8`($sp) # tab[6]=a2^a4 104e1051a39Sopenharmony_ci xgr $a1,$a48 105e1051a39Sopenharmony_ci stg $a12,`$stdframe+7*8`($sp) # tab[7]=a1^a2^a4 106e1051a39Sopenharmony_ci xgr $a2,$a48 107e1051a39Sopenharmony_ci 108e1051a39Sopenharmony_ci stg $a8,`$stdframe+8*8`($sp) # tab[8]=a8 109e1051a39Sopenharmony_ci xgr $a12,$a48 110e1051a39Sopenharmony_ci stg $a1,`$stdframe+9*8`($sp) # tab[9]=a1^a8 111e1051a39Sopenharmony_ci xgr $a1,$a4 112e1051a39Sopenharmony_ci stg $a2,`$stdframe+10*8`($sp) # tab[10]=a2^a8 113e1051a39Sopenharmony_ci xgr $a2,$a4 114e1051a39Sopenharmony_ci stg $a12,`$stdframe+11*8`($sp) # tab[11]=a1^a2^a8 115e1051a39Sopenharmony_ci 116e1051a39Sopenharmony_ci xgr $a12,$a4 117e1051a39Sopenharmony_ci stg $a48,`$stdframe+12*8`($sp) # tab[12]=a4^a8 118e1051a39Sopenharmony_ci srlg $hi,$lo,1 119e1051a39Sopenharmony_ci stg $a1,`$stdframe+13*8`($sp) # tab[13]=a1^a4^a8 120e1051a39Sopenharmony_ci sllg $lo,$lo,63 121e1051a39Sopenharmony_ci stg $a2,`$stdframe+14*8`($sp) # tab[14]=a2^a4^a8 122e1051a39Sopenharmony_ci srlg @T[0],@i[0],2 123e1051a39Sopenharmony_ci stg $a12,`$stdframe+15*8`($sp) # tab[15]=a1^a2^a4^a8 124e1051a39Sopenharmony_ci 125e1051a39Sopenharmony_ci lghi $mask,`0xf<<3` 126e1051a39Sopenharmony_ci sllg $a1,@i[0],62 127e1051a39Sopenharmony_ci sllg @i[0],$b,3 128e1051a39Sopenharmony_ci srlg @T[1],@i[1],3 129e1051a39Sopenharmony_ci ngr @i[0],$mask 130e1051a39Sopenharmony_ci sllg $a2,@i[1],61 131e1051a39Sopenharmony_ci srlg @i[1],$b,4-3 132e1051a39Sopenharmony_ci xgr $hi,@T[0] 133e1051a39Sopenharmony_ci ngr @i[1],$mask 134e1051a39Sopenharmony_ci xgr $lo,$a1 135e1051a39Sopenharmony_ci xgr $hi,@T[1] 136e1051a39Sopenharmony_ci xgr $lo,$a2 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_ci xg $lo,$stdframe(@i[0],$sp) 139e1051a39Sopenharmony_ci srlg @i[0],$b,8-3 140e1051a39Sopenharmony_ci ngr @i[0],$mask 141e1051a39Sopenharmony_ci___ 142e1051a39Sopenharmony_cifor($n=1;$n<14;$n++) { 143e1051a39Sopenharmony_ci$code.=<<___; 144e1051a39Sopenharmony_ci lg @T[1],$stdframe(@i[1],$sp) 145e1051a39Sopenharmony_ci srlg @i[1],$b,`($n+2)*4`-3 146e1051a39Sopenharmony_ci sllg @T[0],@T[1],`$n*4` 147e1051a39Sopenharmony_ci ngr @i[1],$mask 148e1051a39Sopenharmony_ci srlg @T[1],@T[1],`64-$n*4` 149e1051a39Sopenharmony_ci xgr $lo,@T[0] 150e1051a39Sopenharmony_ci xgr $hi,@T[1] 151e1051a39Sopenharmony_ci___ 152e1051a39Sopenharmony_ci push(@i,shift(@i)); push(@T,shift(@T)); 153e1051a39Sopenharmony_ci} 154e1051a39Sopenharmony_ci$code.=<<___; 155e1051a39Sopenharmony_ci lg @T[1],$stdframe(@i[1],$sp) 156e1051a39Sopenharmony_ci sllg @T[0],@T[1],`$n*4` 157e1051a39Sopenharmony_ci srlg @T[1],@T[1],`64-$n*4` 158e1051a39Sopenharmony_ci xgr $lo,@T[0] 159e1051a39Sopenharmony_ci xgr $hi,@T[1] 160e1051a39Sopenharmony_ci 161e1051a39Sopenharmony_ci lg @T[0],$stdframe(@i[0],$sp) 162e1051a39Sopenharmony_ci sllg @T[1],@T[0],`($n+1)*4` 163e1051a39Sopenharmony_ci srlg @T[0],@T[0],`64-($n+1)*4` 164e1051a39Sopenharmony_ci xgr $lo,@T[1] 165e1051a39Sopenharmony_ci xgr $hi,@T[0] 166e1051a39Sopenharmony_ci 167e1051a39Sopenharmony_ci br $ra 168e1051a39Sopenharmony_ci.size _mul_1x1,.-_mul_1x1 169e1051a39Sopenharmony_ci 170e1051a39Sopenharmony_ci.globl bn_GF2m_mul_2x2 171e1051a39Sopenharmony_ci.type bn_GF2m_mul_2x2,\@function 172e1051a39Sopenharmony_ci.align 16 173e1051a39Sopenharmony_cibn_GF2m_mul_2x2: 174e1051a39Sopenharmony_ci stm${g} %r3,%r15,3*$SIZE_T($sp) 175e1051a39Sopenharmony_ci 176e1051a39Sopenharmony_ci lghi %r1,-$stdframe-128 177e1051a39Sopenharmony_ci la %r0,0($sp) 178e1051a39Sopenharmony_ci la $sp,0(%r1,$sp) # alloca 179e1051a39Sopenharmony_ci st${g} %r0,0($sp) # back chain 180e1051a39Sopenharmony_ci___ 181e1051a39Sopenharmony_ciif ($SIZE_T==8) { 182e1051a39Sopenharmony_cimy @r=map("%r$_",(6..9)); 183e1051a39Sopenharmony_ci$code.=<<___; 184e1051a39Sopenharmony_ci bras $ra,_mul_1x1 # a1·b1 185e1051a39Sopenharmony_ci stmg $lo,$hi,16($rp) 186e1051a39Sopenharmony_ci 187e1051a39Sopenharmony_ci lg $a,`$stdframe+128+4*$SIZE_T`($sp) 188e1051a39Sopenharmony_ci lg $b,`$stdframe+128+6*$SIZE_T`($sp) 189e1051a39Sopenharmony_ci bras $ra,_mul_1x1 # a0·b0 190e1051a39Sopenharmony_ci stmg $lo,$hi,0($rp) 191e1051a39Sopenharmony_ci 192e1051a39Sopenharmony_ci lg $a,`$stdframe+128+3*$SIZE_T`($sp) 193e1051a39Sopenharmony_ci lg $b,`$stdframe+128+5*$SIZE_T`($sp) 194e1051a39Sopenharmony_ci xg $a,`$stdframe+128+4*$SIZE_T`($sp) 195e1051a39Sopenharmony_ci xg $b,`$stdframe+128+6*$SIZE_T`($sp) 196e1051a39Sopenharmony_ci bras $ra,_mul_1x1 # (a0+a1)·(b0+b1) 197e1051a39Sopenharmony_ci lmg @r[0],@r[3],0($rp) 198e1051a39Sopenharmony_ci 199e1051a39Sopenharmony_ci xgr $lo,$hi 200e1051a39Sopenharmony_ci xgr $hi,@r[1] 201e1051a39Sopenharmony_ci xgr $lo,@r[0] 202e1051a39Sopenharmony_ci xgr $hi,@r[2] 203e1051a39Sopenharmony_ci xgr $lo,@r[3] 204e1051a39Sopenharmony_ci xgr $hi,@r[3] 205e1051a39Sopenharmony_ci xgr $lo,$hi 206e1051a39Sopenharmony_ci stg $hi,16($rp) 207e1051a39Sopenharmony_ci stg $lo,8($rp) 208e1051a39Sopenharmony_ci___ 209e1051a39Sopenharmony_ci} else { 210e1051a39Sopenharmony_ci$code.=<<___; 211e1051a39Sopenharmony_ci sllg %r3,%r3,32 212e1051a39Sopenharmony_ci sllg %r5,%r5,32 213e1051a39Sopenharmony_ci or %r3,%r4 214e1051a39Sopenharmony_ci or %r5,%r6 215e1051a39Sopenharmony_ci bras $ra,_mul_1x1 216e1051a39Sopenharmony_ci rllg $lo,$lo,32 217e1051a39Sopenharmony_ci rllg $hi,$hi,32 218e1051a39Sopenharmony_ci stmg $lo,$hi,0($rp) 219e1051a39Sopenharmony_ci___ 220e1051a39Sopenharmony_ci} 221e1051a39Sopenharmony_ci$code.=<<___; 222e1051a39Sopenharmony_ci lm${g} %r6,%r15,`$stdframe+128+6*$SIZE_T`($sp) 223e1051a39Sopenharmony_ci br $ra 224e1051a39Sopenharmony_ci.size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2 225e1051a39Sopenharmony_ci.string "GF(2^m) Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>" 226e1051a39Sopenharmony_ci___ 227e1051a39Sopenharmony_ci 228e1051a39Sopenharmony_ci$code =~ s/\`([^\`]*)\`/eval($1)/gem; 229e1051a39Sopenharmony_ciprint $code; 230e1051a39Sopenharmony_ciclose STDOUT or die "error closing STDOUT: $!"; 231