2016-05-21 20:23:39 +08:00
|
|
|
#! /usr/bin/env perl
|
2020-04-23 20:55:52 +08:00
|
|
|
# Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-21 20:23:39 +08:00
|
|
|
#
|
2018-12-06 20:22:12 +08:00
|
|
|
# Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-21 20:23:39 +08:00
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
|
|
|
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
# ====================================================================
|
2017-10-11 05:55:09 +08:00
|
|
|
# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
|
2007-04-30 16:42:54 +08:00
|
|
|
# project. The module is, however, dual licensed under OpenSSL and
|
|
|
|
# CRYPTOGAMS licenses depending on where you obtain it. For further
|
|
|
|
# details see http://www.openssl.org/~appro/cryptogams/.
|
|
|
|
# ====================================================================
|
|
|
|
|
|
|
|
# April 2007.
|
|
|
|
#
|
|
|
|
# Performance improvement over vanilla C code varies from 85% to 45%
|
|
|
|
# depending on key length and benchmark. Unfortunately in this context
|
|
|
|
# these are not very impressive results [for code that utilizes "wide"
|
|
|
|
# 64x64=128-bit multiplication, which is not commonly available to C
|
|
|
|
# programmers], at least hand-coded bn_asm.c replacement is known to
|
|
|
|
# provide 30-40% better results for longest keys. Well, on a second
|
|
|
|
# thought it's not very surprising, because z-CPUs are single-issue
|
|
|
|
# and _strictly_ in-order execution, while bn_mul_mont is more or less
|
|
|
|
# dependent on CPU ability to pipe-line instructions and have several
|
|
|
|
# of them "in-flight" at the same time. I mean while other methods,
|
|
|
|
# for example Karatsuba, aim to minimize amount of multiplications at
|
|
|
|
# the cost of other operations increase, bn_mul_mont aim to neatly
|
|
|
|
# "overlap" multiplications and the other operations [and on most
|
|
|
|
# platforms even minimize the amount of the other operations, in
|
|
|
|
# particular references to memory]. But it's possible to improve this
|
|
|
|
# module performance by implementing dedicated squaring code-path and
|
|
|
|
# possibly by unrolling loops...
|
|
|
|
|
2009-02-09 23:42:04 +08:00
|
|
|
# January 2009.
|
|
|
|
#
|
|
|
|
# Reschedule to minimize/avoid Address Generation Interlock hazard,
|
|
|
|
# make inner loops counter-based.
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
# November 2010.
|
|
|
|
#
|
|
|
|
# Adapt for -m31 build. If kernel supports what's called "highgprs"
|
|
|
|
# feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
|
|
|
|
# instructions and achieve "64-bit" performance even in 31-bit legacy
|
|
|
|
# application context. The feature is not specific to any particular
|
|
|
|
# processor, as long as it's "z-CPU". Latter implies that the code
|
|
|
|
# remains z/Architecture specific. Compatibility with 32-bit BN_ULONG
|
|
|
|
# is achieved by swapping words after 64-bit loads, follow _dswap-s.
|
2011-03-04 21:09:16 +08:00
|
|
|
# On z990 it was measured to perform 2.6-2.2 times better than
|
|
|
|
# compiler-generated code, less for longer keys...
|
2010-11-30 04:52:43 +08:00
|
|
|
|
Unify all assembler file generators
They now generally conform to the following argument sequence:
script.pl "$(PERLASM_SCHEME)" [ C preprocessor arguments ... ] \
$(PROCESSOR) <output file>
However, in the spirit of being able to use these scripts manually,
they also allow for no argument, or for only the flavour, or for only
the output file. This is done by only using the last argument as
output file if it's a file (it has an extension), and only using the
first argument as flavour if it isn't a file (it doesn't have an
extension).
While we're at it, we make all $xlate calls the same, i.e. the $output
argument is always quoted, and we always die on error when trying to
start $xlate.
There's a perl lesson in this, regarding operator priority...
This will always succeed, even when it fails:
open FOO, "something" || die "ERR: $!";
The reason is that '||' has higher priority than list operators (a
function is essentially a list operator and gobbles up everything
following it that isn't lower priority), and since a non-empty string
is always true, so that ends up being exactly the same as:
open FOO, "something";
This, however, will fail if "something" can't be opened:
open FOO, "something" or die "ERR: $!";
The reason is that 'or' has lower priority that list operators,
i.e. it's performed after the 'open' call.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9884)
2019-09-13 06:06:46 +08:00
|
|
|
# $output is the last argument if it looks like a file (it has an extension)
|
|
|
|
# $flavour is the first argument if it doesn't look like a file
|
|
|
|
$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
|
|
|
|
$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
|
2010-11-30 04:52:43 +08:00
|
|
|
|
|
|
|
if ($flavour =~ /3[12]/) {
|
|
|
|
$SIZE_T=4;
|
|
|
|
$g="";
|
|
|
|
} else {
|
|
|
|
$SIZE_T=8;
|
|
|
|
$g="g";
|
|
|
|
}
|
|
|
|
|
Unify all assembler file generators
They now generally conform to the following argument sequence:
script.pl "$(PERLASM_SCHEME)" [ C preprocessor arguments ... ] \
$(PROCESSOR) <output file>
However, in the spirit of being able to use these scripts manually,
they also allow for no argument, or for only the flavour, or for only
the output file. This is done by only using the last argument as
output file if it's a file (it has an extension), and only using the
first argument as flavour if it isn't a file (it doesn't have an
extension).
While we're at it, we make all $xlate calls the same, i.e. the $output
argument is always quoted, and we always die on error when trying to
start $xlate.
There's a perl lesson in this, regarding operator priority...
This will always succeed, even when it fails:
open FOO, "something" || die "ERR: $!";
The reason is that '||' has higher priority than list operators (a
function is essentially a list operator and gobbles up everything
following it that isn't lower priority), and since a non-empty string
is always true, so that ends up being exactly the same as:
open FOO, "something";
This, however, will fail if "something" can't be opened:
open FOO, "something" or die "ERR: $!";
The reason is that 'or' has lower priority that list operators,
i.e. it's performed after the 'open' call.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9884)
2019-09-13 06:06:46 +08:00
|
|
|
$output and open STDOUT,">$output";
|
2010-07-09 20:11:12 +08:00
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
$stdframe=16*$SIZE_T+4*8;
|
|
|
|
|
2007-04-30 16:42:54 +08:00
|
|
|
$mn0="%r0";
|
|
|
|
$num="%r1";
|
|
|
|
|
|
|
|
# int bn_mul_mont(
|
|
|
|
$rp="%r2"; # BN_ULONG *rp,
|
|
|
|
$ap="%r3"; # const BN_ULONG *ap,
|
|
|
|
$bp="%r4"; # const BN_ULONG *bp,
|
|
|
|
$np="%r5"; # const BN_ULONG *np,
|
|
|
|
$n0="%r6"; # const BN_ULONG *n0,
|
|
|
|
#$num="160(%r15)" # int num);
|
|
|
|
|
|
|
|
$bi="%r2"; # zaps rp
|
|
|
|
$j="%r7";
|
|
|
|
|
|
|
|
$ahi="%r8";
|
|
|
|
$alo="%r9";
|
|
|
|
$nhi="%r10";
|
|
|
|
$nlo="%r11";
|
|
|
|
$AHI="%r12";
|
|
|
|
$NHI="%r13";
|
2009-02-09 23:42:04 +08:00
|
|
|
$count="%r14";
|
2007-04-30 16:42:54 +08:00
|
|
|
$sp="%r15";
|
|
|
|
|
|
|
|
$code.=<<___;
|
|
|
|
.text
|
|
|
|
.globl bn_mul_mont
|
|
|
|
.type bn_mul_mont,\@function
|
|
|
|
bn_mul_mont:
|
2010-11-30 04:52:43 +08:00
|
|
|
lgf $num,`$stdframe+$SIZE_T-4`($sp) # pull $num
|
|
|
|
sla $num,`log($SIZE_T)/log(2)` # $num to enumerate bytes
|
2007-04-30 16:42:54 +08:00
|
|
|
la $bp,0($num,$bp)
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
st${g} %r2,2*$SIZE_T($sp)
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
cghi $num,16 #
|
|
|
|
lghi %r2,0 #
|
|
|
|
blr %r14 # if($num<16) return 0;
|
2010-11-30 04:52:43 +08:00
|
|
|
___
|
|
|
|
$code.=<<___ if ($flavour =~ /3[12]/);
|
|
|
|
tmll $num,4
|
|
|
|
bnzr %r14 # if ($num&1) return 0;
|
|
|
|
___
|
|
|
|
$code.=<<___ if ($flavour !~ /3[12]/);
|
2011-03-04 21:09:16 +08:00
|
|
|
cghi $num,96 #
|
|
|
|
bhr %r14 # if($num>96) return 0;
|
2010-11-30 04:52:43 +08:00
|
|
|
___
|
|
|
|
$code.=<<___;
|
|
|
|
stm${g} %r3,%r15,3*$SIZE_T($sp)
|
2007-04-30 16:42:54 +08:00
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
lghi $rp,-$stdframe-8 # leave room for carry bit
|
2009-02-09 23:42:04 +08:00
|
|
|
lcgr $j,$num # -$num
|
2007-04-30 16:42:54 +08:00
|
|
|
lgr %r0,$sp
|
2009-02-09 23:42:04 +08:00
|
|
|
la $rp,0($rp,$sp)
|
|
|
|
la $sp,0($j,$rp) # alloca
|
2010-11-30 04:52:43 +08:00
|
|
|
st${g} %r0,0($sp) # back chain
|
2007-04-30 16:42:54 +08:00
|
|
|
|
2009-02-09 23:42:04 +08:00
|
|
|
sra $num,3 # restore $num
|
|
|
|
la $bp,0($j,$bp) # restore $bp
|
|
|
|
ahi $num,-1 # adjust $num for inner loop
|
2007-04-30 16:42:54 +08:00
|
|
|
lg $n0,0($n0) # pull n0
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $n0
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
lg $bi,0($bp)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $bi
|
2009-02-09 23:42:04 +08:00
|
|
|
lg $alo,0($ap)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $alo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $ahi,$bi # ap[0]*bp[0]
|
|
|
|
lgr $AHI,$ahi
|
|
|
|
|
|
|
|
lgr $mn0,$alo # "tp[0]"*n0
|
|
|
|
msgr $mn0,$n0
|
|
|
|
|
2009-02-09 23:42:04 +08:00
|
|
|
lg $nlo,0($np) #
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $nlo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $nhi,$mn0 # np[0]*m1
|
|
|
|
algr $nlo,$alo # +="tp[0]"
|
|
|
|
lghi $NHI,0
|
|
|
|
alcgr $NHI,$nhi
|
|
|
|
|
2019-11-02 06:29:04 +08:00
|
|
|
la $j,8 # j=1
|
2009-02-09 23:42:04 +08:00
|
|
|
lr $count,$num
|
|
|
|
|
|
|
|
.align 16
|
2007-04-30 16:42:54 +08:00
|
|
|
.L1st:
|
|
|
|
lg $alo,0($j,$ap)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $alo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $ahi,$bi # ap[j]*bp[0]
|
|
|
|
algr $alo,$AHI
|
|
|
|
lghi $AHI,0
|
|
|
|
alcgr $AHI,$ahi
|
|
|
|
|
|
|
|
lg $nlo,0($j,$np)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $nlo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $nhi,$mn0 # np[j]*m1
|
|
|
|
algr $nlo,$NHI
|
|
|
|
lghi $NHI,0
|
|
|
|
alcgr $nhi,$NHI # +="tp[j]"
|
|
|
|
algr $nlo,$alo
|
|
|
|
alcgr $NHI,$nhi
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
|
2009-02-09 23:42:04 +08:00
|
|
|
la $j,8($j) # j++
|
|
|
|
brct $count,.L1st
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
algr $NHI,$AHI
|
|
|
|
lghi $AHI,0
|
|
|
|
alcgr $AHI,$AHI # upmost overflow bit
|
2010-11-30 04:52:43 +08:00
|
|
|
stg $NHI,$stdframe-8($j,$sp)
|
|
|
|
stg $AHI,$stdframe($j,$sp)
|
2007-04-30 16:42:54 +08:00
|
|
|
la $bp,8($bp) # bp++
|
|
|
|
|
|
|
|
.Louter:
|
|
|
|
lg $bi,0($bp) # bp[i]
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $bi
|
2009-02-09 23:42:04 +08:00
|
|
|
lg $alo,0($ap)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $alo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $ahi,$bi # ap[0]*bp[i]
|
2010-11-30 04:52:43 +08:00
|
|
|
alg $alo,$stdframe($sp) # +=tp[0]
|
2007-04-30 16:42:54 +08:00
|
|
|
lghi $AHI,0
|
|
|
|
alcgr $AHI,$ahi
|
|
|
|
|
|
|
|
lgr $mn0,$alo
|
2009-02-09 23:42:04 +08:00
|
|
|
msgr $mn0,$n0 # tp[0]*n0
|
2007-04-30 16:42:54 +08:00
|
|
|
|
2009-02-09 23:42:04 +08:00
|
|
|
lg $nlo,0($np) # np[0]
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $nlo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $nhi,$mn0 # np[0]*m1
|
|
|
|
algr $nlo,$alo # +="tp[0]"
|
|
|
|
lghi $NHI,0
|
|
|
|
alcgr $NHI,$nhi
|
|
|
|
|
2019-11-02 06:29:04 +08:00
|
|
|
la $j,8 # j=1
|
2009-02-09 23:42:04 +08:00
|
|
|
lr $count,$num
|
|
|
|
|
|
|
|
.align 16
|
2007-04-30 16:42:54 +08:00
|
|
|
.Linner:
|
|
|
|
lg $alo,0($j,$ap)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $alo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $ahi,$bi # ap[j]*bp[i]
|
|
|
|
algr $alo,$AHI
|
|
|
|
lghi $AHI,0
|
|
|
|
alcgr $ahi,$AHI
|
2010-11-30 04:52:43 +08:00
|
|
|
alg $alo,$stdframe($j,$sp)# +=tp[j]
|
2007-04-30 16:42:54 +08:00
|
|
|
alcgr $AHI,$ahi
|
|
|
|
|
|
|
|
lg $nlo,0($j,$np)
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $nlo
|
2007-04-30 16:42:54 +08:00
|
|
|
mlgr $nhi,$mn0 # np[j]*m1
|
|
|
|
algr $nlo,$NHI
|
|
|
|
lghi $NHI,0
|
|
|
|
alcgr $nhi,$NHI
|
|
|
|
algr $nlo,$alo # +="tp[j]"
|
|
|
|
alcgr $NHI,$nhi
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
|
2009-02-09 23:42:04 +08:00
|
|
|
la $j,8($j) # j++
|
|
|
|
brct $count,.Linner
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
algr $NHI,$AHI
|
|
|
|
lghi $AHI,0
|
|
|
|
alcgr $AHI,$AHI
|
2010-11-30 04:52:43 +08:00
|
|
|
alg $NHI,$stdframe($j,$sp)# accumulate previous upmost overflow bit
|
2007-04-30 16:42:54 +08:00
|
|
|
lghi $ahi,0
|
|
|
|
alcgr $AHI,$ahi # new upmost overflow bit
|
2010-11-30 04:52:43 +08:00
|
|
|
stg $NHI,$stdframe-8($j,$sp)
|
|
|
|
stg $AHI,$stdframe($j,$sp)
|
2007-04-30 16:42:54 +08:00
|
|
|
|
|
|
|
la $bp,8($bp) # bp++
|
2010-11-30 04:52:43 +08:00
|
|
|
cl${g} $bp,`$stdframe+8+4*$SIZE_T`($j,$sp) # compare to &bp[num]
|
2007-04-30 16:42:54 +08:00
|
|
|
jne .Louter
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
l${g} $rp,`$stdframe+8+2*$SIZE_T`($j,$sp) # reincarnate rp
|
|
|
|
la $ap,$stdframe($sp)
|
2009-02-09 23:42:04 +08:00
|
|
|
ahi $num,1 # restore $num, incidentally clears "borrow"
|
2007-04-30 16:42:54 +08:00
|
|
|
|
2019-11-02 06:29:04 +08:00
|
|
|
la $j,0
|
2009-02-09 23:42:04 +08:00
|
|
|
lr $count,$num
|
2007-06-18 01:10:03 +08:00
|
|
|
.Lsub: lg $alo,0($j,$ap)
|
2010-11-30 04:52:43 +08:00
|
|
|
lg $nlo,0($j,$np)
|
|
|
|
_dswap $nlo
|
|
|
|
slbgr $alo,$nlo
|
2007-04-30 16:42:54 +08:00
|
|
|
stg $alo,0($j,$rp)
|
|
|
|
la $j,8($j)
|
2007-06-18 01:10:03 +08:00
|
|
|
brct $count,.Lsub
|
2007-04-30 16:42:54 +08:00
|
|
|
lghi $ahi,0
|
2007-06-18 01:10:03 +08:00
|
|
|
slbgr $AHI,$ahi # handle upmost carry
|
2018-05-01 04:59:51 +08:00
|
|
|
lghi $NHI,-1
|
|
|
|
xgr $NHI,$AHI
|
2007-04-30 16:42:54 +08:00
|
|
|
|
2019-11-02 06:29:04 +08:00
|
|
|
la $j,0
|
2009-02-09 23:42:04 +08:00
|
|
|
lgr $count,$num
|
2018-05-01 04:59:51 +08:00
|
|
|
.Lcopy: lg $ahi,$stdframe($j,$sp) # conditional copy
|
|
|
|
lg $alo,0($j,$rp)
|
|
|
|
ngr $ahi,$AHI
|
|
|
|
ngr $alo,$NHI
|
|
|
|
ogr $alo,$ahi
|
2010-11-30 04:52:43 +08:00
|
|
|
_dswap $alo
|
|
|
|
stg $j,$stdframe($j,$sp) # zap tp
|
2007-06-18 01:10:03 +08:00
|
|
|
stg $alo,0($j,$rp)
|
2009-02-09 23:42:04 +08:00
|
|
|
la $j,8($j)
|
|
|
|
brct $count,.Lcopy
|
2007-06-18 01:10:03 +08:00
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
la %r1,`$stdframe+8+6*$SIZE_T`($j,$sp)
|
|
|
|
lm${g} %r6,%r15,0(%r1)
|
2007-06-18 01:10:03 +08:00
|
|
|
lghi %r2,1 # signal "processed"
|
|
|
|
br %r14
|
2007-04-30 16:42:54 +08:00
|
|
|
.size bn_mul_mont,.-bn_mul_mont
|
|
|
|
.string "Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
|
|
|
|
___
|
|
|
|
|
2010-11-30 04:52:43 +08:00
|
|
|
foreach (split("\n",$code)) {
|
|
|
|
s/\`([^\`]*)\`/eval $1/ge;
|
|
|
|
s/_dswap\s+(%r[0-9]+)/sprintf("rllg\t%s,%s,32",$1,$1) if($SIZE_T==4)/e;
|
|
|
|
print $_,"\n";
|
|
|
|
}
|
2020-02-17 10:17:53 +08:00
|
|
|
close STDOUT or die "error closing STDOUT: $!";
|