2016-05-21 12:23:39 +00:00
|
|
|
|
#! /usr/bin/env perl
|
|
|
|
|
# Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
|
# 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
|
|
|
|
|
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
|
|
|
|
# ====================================================================
|
|
|
|
|
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
|
2007-06-17 17:10:03 +00: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/.
|
2006-04-30 21:15:29 +00:00
|
|
|
|
# ====================================================================
|
|
|
|
|
|
|
|
|
|
# April 2006
|
|
|
|
|
|
|
|
|
|
# "Teaser" Montgomery multiplication module for PowerPC. It's possible
|
|
|
|
|
# to gain a bit more by modulo-scheduling outer loop, then dedicated
|
|
|
|
|
# squaring procedure should give further 20% and code can be adapted
|
|
|
|
|
# for 32-bit application running on 64-bit CPU. As for the latter.
|
|
|
|
|
# It won't be able to achieve "native" 64-bit performance, because in
|
|
|
|
|
# 32-bit application context every addc instruction will have to be
|
|
|
|
|
# expanded as addc, twice right shift by 32 and finally adde, etc.
|
|
|
|
|
# So far RSA *sign* performance improvement over pre-bn_mul_mont asm
|
|
|
|
|
# for 64-bit application running on PPC970/G5 is:
|
|
|
|
|
#
|
|
|
|
|
# 512-bit +65%
|
|
|
|
|
# 1024-bit +35%
|
|
|
|
|
# 2048-bit +18%
|
|
|
|
|
# 4096-bit +4%
|
|
|
|
|
|
2008-01-13 22:01:30 +00:00
|
|
|
|
$flavour = shift;
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
2008-01-13 22:01:30 +00:00
|
|
|
|
if ($flavour =~ /32/) {
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$BITS= 32;
|
|
|
|
|
$BNSZ= $BITS/8;
|
|
|
|
|
$SIZE_T=4;
|
|
|
|
|
$RZONE= 224;
|
|
|
|
|
|
|
|
|
|
$LD= "lwz"; # load
|
|
|
|
|
$LDU= "lwzu"; # load and update
|
|
|
|
|
$LDX= "lwzx"; # load indexed
|
|
|
|
|
$ST= "stw"; # store
|
|
|
|
|
$STU= "stwu"; # store and update
|
|
|
|
|
$STX= "stwx"; # store indexed
|
|
|
|
|
$STUX= "stwux"; # store indexed and update
|
|
|
|
|
$UMULL= "mullw"; # unsigned multiply low
|
|
|
|
|
$UMULH= "mulhwu"; # unsigned multiply high
|
|
|
|
|
$UCMP= "cmplw"; # unsigned compare
|
2007-06-17 17:10:03 +00:00
|
|
|
|
$SHRI= "srwi"; # unsigned shift right by immediate
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$PUSH= $ST;
|
|
|
|
|
$POP= $LD;
|
2008-01-13 22:01:30 +00:00
|
|
|
|
} elsif ($flavour =~ /64/) {
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$BITS= 64;
|
|
|
|
|
$BNSZ= $BITS/8;
|
|
|
|
|
$SIZE_T=8;
|
|
|
|
|
$RZONE= 288;
|
|
|
|
|
|
|
|
|
|
# same as above, but 64-bit mnemonics...
|
|
|
|
|
$LD= "ld"; # load
|
|
|
|
|
$LDU= "ldu"; # load and update
|
|
|
|
|
$LDX= "ldx"; # load indexed
|
|
|
|
|
$ST= "std"; # store
|
|
|
|
|
$STU= "stdu"; # store and update
|
|
|
|
|
$STX= "stdx"; # store indexed
|
|
|
|
|
$STUX= "stdux"; # store indexed and update
|
|
|
|
|
$UMULL= "mulld"; # unsigned multiply low
|
|
|
|
|
$UMULH= "mulhdu"; # unsigned multiply high
|
|
|
|
|
$UCMP= "cmpld"; # unsigned compare
|
2007-06-17 17:10:03 +00:00
|
|
|
|
$SHRI= "srdi"; # unsigned shift right by immediate
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$PUSH= $ST;
|
|
|
|
|
$POP= $LD;
|
2008-01-13 22:01:30 +00:00
|
|
|
|
} else { die "nonsense $flavour"; }
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
2011-05-27 13:32:34 +00:00
|
|
|
|
$FRAME=8*$SIZE_T+$RZONE;
|
|
|
|
|
$LOCALS=8*$SIZE_T;
|
|
|
|
|
|
2008-01-13 22:01:30 +00:00
|
|
|
|
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
|
|
|
|
|
( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
|
|
|
|
|
( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
|
|
|
|
|
die "can't locate ppc-xlate.pl";
|
|
|
|
|
|
|
|
|
|
open STDOUT,"| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
|
|
|
|
$sp="r1";
|
|
|
|
|
$toc="r2";
|
|
|
|
|
$rp="r3"; $ovf="r3";
|
|
|
|
|
$ap="r4";
|
|
|
|
|
$bp="r5";
|
|
|
|
|
$np="r6";
|
|
|
|
|
$n0="r7";
|
|
|
|
|
$num="r8";
|
|
|
|
|
$rp="r9"; # $rp is reassigned
|
|
|
|
|
$aj="r10";
|
|
|
|
|
$nj="r11";
|
|
|
|
|
$tj="r12";
|
|
|
|
|
# non-volatile registers
|
2011-05-27 13:32:34 +00:00
|
|
|
|
$i="r20";
|
|
|
|
|
$j="r21";
|
|
|
|
|
$tp="r22";
|
|
|
|
|
$m0="r23";
|
|
|
|
|
$m1="r24";
|
|
|
|
|
$lo0="r25";
|
|
|
|
|
$hi0="r26";
|
|
|
|
|
$lo1="r27";
|
|
|
|
|
$hi1="r28";
|
|
|
|
|
$alo="r29";
|
|
|
|
|
$ahi="r30";
|
|
|
|
|
$nlo="r31";
|
2006-04-30 21:15:29 +00:00
|
|
|
|
#
|
|
|
|
|
$nhi="r0";
|
|
|
|
|
|
|
|
|
|
$code=<<___;
|
2006-05-04 21:30:41 +00:00
|
|
|
|
.machine "any"
|
2006-04-30 21:15:29 +00:00
|
|
|
|
.text
|
|
|
|
|
|
2009-12-26 21:30:13 +00:00
|
|
|
|
.globl .bn_mul_mont_int
|
2006-04-30 21:15:29 +00:00
|
|
|
|
.align 4
|
2009-12-26 21:30:13 +00:00
|
|
|
|
.bn_mul_mont_int:
|
2006-04-30 21:15:29 +00:00
|
|
|
|
cmpwi $num,4
|
|
|
|
|
mr $rp,r3 ; $rp is reassigned
|
|
|
|
|
li r3,0
|
|
|
|
|
bltlr
|
2009-12-26 21:30:13 +00:00
|
|
|
|
___
|
|
|
|
|
$code.=<<___ if ($BNSZ==4);
|
|
|
|
|
cmpwi $num,32 ; longer key performance is not better
|
|
|
|
|
bgelr
|
|
|
|
|
___
|
|
|
|
|
$code.=<<___;
|
2006-04-30 21:15:29 +00:00
|
|
|
|
slwi $num,$num,`log($BNSZ)/log(2)`
|
|
|
|
|
li $tj,-4096
|
2011-05-27 13:32:34 +00:00
|
|
|
|
addi $ovf,$num,$FRAME
|
2006-04-30 21:15:29 +00:00
|
|
|
|
subf $ovf,$ovf,$sp ; $sp-$ovf
|
|
|
|
|
and $ovf,$ovf,$tj ; minimize TLB usage
|
|
|
|
|
subf $ovf,$sp,$ovf ; $ovf-$sp
|
2011-05-27 13:32:34 +00:00
|
|
|
|
mr $tj,$sp
|
2006-04-30 21:15:29 +00:00
|
|
|
|
srwi $num,$num,`log($BNSZ)/log(2)`
|
|
|
|
|
$STUX $sp,$sp,$ovf
|
|
|
|
|
|
2011-05-27 13:32:34 +00:00
|
|
|
|
$PUSH r20,`-12*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r21,`-11*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r22,`-10*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r23,`-9*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r24,`-8*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r25,`-7*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r26,`-6*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r27,`-5*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r28,`-4*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r29,`-3*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r30,`-2*$SIZE_T`($tj)
|
|
|
|
|
$PUSH r31,`-1*$SIZE_T`($tj)
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
|
|
|
|
$LD $n0,0($n0) ; pull n0[0] value
|
|
|
|
|
addi $num,$num,-2 ; adjust $num for counter register
|
|
|
|
|
|
|
|
|
|
$LD $m0,0($bp) ; m0=bp[0]
|
|
|
|
|
$LD $aj,0($ap) ; ap[0]
|
2011-05-27 13:32:34 +00:00
|
|
|
|
addi $tp,$sp,$LOCALS
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$UMULL $lo0,$aj,$m0 ; ap[0]*bp[0]
|
|
|
|
|
$UMULH $hi0,$aj,$m0
|
|
|
|
|
|
|
|
|
|
$LD $aj,$BNSZ($ap) ; ap[1]
|
|
|
|
|
$LD $nj,0($np) ; np[0]
|
|
|
|
|
|
|
|
|
|
$UMULL $m1,$lo0,$n0 ; "tp[0]"*n0
|
|
|
|
|
|
|
|
|
|
$UMULL $alo,$aj,$m0 ; ap[1]*bp[0]
|
|
|
|
|
$UMULH $ahi,$aj,$m0
|
|
|
|
|
|
|
|
|
|
$UMULL $lo1,$nj,$m1 ; np[0]*m1
|
|
|
|
|
$UMULH $hi1,$nj,$m1
|
|
|
|
|
$LD $nj,$BNSZ($np) ; np[1]
|
|
|
|
|
addc $lo1,$lo1,$lo0
|
|
|
|
|
addze $hi1,$hi1
|
|
|
|
|
|
|
|
|
|
$UMULL $nlo,$nj,$m1 ; np[1]*m1
|
|
|
|
|
$UMULH $nhi,$nj,$m1
|
|
|
|
|
|
|
|
|
|
mtctr $num
|
|
|
|
|
li $j,`2*$BNSZ`
|
|
|
|
|
.align 4
|
|
|
|
|
L1st:
|
|
|
|
|
$LDX $aj,$ap,$j ; ap[j]
|
|
|
|
|
addc $lo0,$alo,$hi0
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$LDX $nj,$np,$j ; np[j]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi0,$ahi
|
|
|
|
|
$UMULL $alo,$aj,$m0 ; ap[j]*bp[0]
|
|
|
|
|
addc $lo1,$nlo,$hi1
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$UMULH $ahi,$aj,$m0
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi1,$nhi
|
|
|
|
|
$UMULL $nlo,$nj,$m1 ; np[j]*m1
|
|
|
|
|
addc $lo1,$lo1,$lo0 ; np[j]*m1+ap[j]*bp[0]
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$UMULH $nhi,$nj,$m1
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi1,$hi1
|
|
|
|
|
$ST $lo1,0($tp) ; tp[j-1]
|
|
|
|
|
|
|
|
|
|
addi $j,$j,$BNSZ ; j++
|
|
|
|
|
addi $tp,$tp,$BNSZ ; tp++
|
2016-03-31 16:47:17 +00:00
|
|
|
|
bdnz L1st
|
2006-04-30 21:15:29 +00:00
|
|
|
|
;L1st
|
|
|
|
|
addc $lo0,$alo,$hi0
|
|
|
|
|
addze $hi0,$ahi
|
|
|
|
|
|
|
|
|
|
addc $lo1,$nlo,$hi1
|
|
|
|
|
addze $hi1,$nhi
|
|
|
|
|
addc $lo1,$lo1,$lo0 ; np[j]*m1+ap[j]*bp[0]
|
|
|
|
|
addze $hi1,$hi1
|
|
|
|
|
$ST $lo1,0($tp) ; tp[j-1]
|
|
|
|
|
|
|
|
|
|
li $ovf,0
|
|
|
|
|
addc $hi1,$hi1,$hi0
|
|
|
|
|
addze $ovf,$ovf ; upmost overflow bit
|
|
|
|
|
$ST $hi1,$BNSZ($tp)
|
|
|
|
|
|
|
|
|
|
li $i,$BNSZ
|
|
|
|
|
.align 4
|
|
|
|
|
Louter:
|
|
|
|
|
$LDX $m0,$bp,$i ; m0=bp[i]
|
|
|
|
|
$LD $aj,0($ap) ; ap[0]
|
2011-05-27 13:32:34 +00:00
|
|
|
|
addi $tp,$sp,$LOCALS
|
|
|
|
|
$LD $tj,$LOCALS($sp); tp[0]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$UMULL $lo0,$aj,$m0 ; ap[0]*bp[i]
|
|
|
|
|
$UMULH $hi0,$aj,$m0
|
|
|
|
|
$LD $aj,$BNSZ($ap) ; ap[1]
|
|
|
|
|
$LD $nj,0($np) ; np[0]
|
|
|
|
|
addc $lo0,$lo0,$tj ; ap[0]*bp[i]+tp[0]
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$UMULL $alo,$aj,$m0 ; ap[j]*bp[i]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi0,$hi0
|
|
|
|
|
$UMULL $m1,$lo0,$n0 ; tp[0]*n0
|
|
|
|
|
$UMULH $ahi,$aj,$m0
|
|
|
|
|
$UMULL $lo1,$nj,$m1 ; np[0]*m1
|
|
|
|
|
$UMULH $hi1,$nj,$m1
|
|
|
|
|
$LD $nj,$BNSZ($np) ; np[1]
|
|
|
|
|
addc $lo1,$lo1,$lo0
|
|
|
|
|
$UMULL $nlo,$nj,$m1 ; np[1]*m1
|
2006-08-09 15:40:30 +00:00
|
|
|
|
addze $hi1,$hi1
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$UMULH $nhi,$nj,$m1
|
|
|
|
|
|
|
|
|
|
mtctr $num
|
|
|
|
|
li $j,`2*$BNSZ`
|
|
|
|
|
.align 4
|
|
|
|
|
Linner:
|
|
|
|
|
$LDX $aj,$ap,$j ; ap[j]
|
|
|
|
|
addc $lo0,$alo,$hi0
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$LD $tj,$BNSZ($tp) ; tp[j]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi0,$ahi
|
|
|
|
|
$LDX $nj,$np,$j ; np[j]
|
|
|
|
|
addc $lo1,$nlo,$hi1
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$UMULL $alo,$aj,$m0 ; ap[j]*bp[i]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi1,$nhi
|
2006-08-09 15:40:30 +00:00
|
|
|
|
$UMULH $ahi,$aj,$m0
|
|
|
|
|
addc $lo0,$lo0,$tj ; ap[j]*bp[i]+tp[j]
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$UMULL $nlo,$nj,$m1 ; np[j]*m1
|
2006-08-09 15:40:30 +00:00
|
|
|
|
addze $hi0,$hi0
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$UMULH $nhi,$nj,$m1
|
|
|
|
|
addc $lo1,$lo1,$lo0 ; np[j]*m1+ap[j]*bp[i]+tp[j]
|
2006-08-09 15:40:30 +00:00
|
|
|
|
addi $j,$j,$BNSZ ; j++
|
2006-04-30 21:15:29 +00:00
|
|
|
|
addze $hi1,$hi1
|
|
|
|
|
$ST $lo1,0($tp) ; tp[j-1]
|
|
|
|
|
addi $tp,$tp,$BNSZ ; tp++
|
2016-03-31 16:47:17 +00:00
|
|
|
|
bdnz Linner
|
2006-04-30 21:15:29 +00:00
|
|
|
|
;Linner
|
|
|
|
|
$LD $tj,$BNSZ($tp) ; tp[j]
|
|
|
|
|
addc $lo0,$alo,$hi0
|
|
|
|
|
addze $hi0,$ahi
|
|
|
|
|
addc $lo0,$lo0,$tj ; ap[j]*bp[i]+tp[j]
|
|
|
|
|
addze $hi0,$hi0
|
|
|
|
|
|
|
|
|
|
addc $lo1,$nlo,$hi1
|
|
|
|
|
addze $hi1,$nhi
|
|
|
|
|
addc $lo1,$lo1,$lo0 ; np[j]*m1+ap[j]*bp[i]+tp[j]
|
|
|
|
|
addze $hi1,$hi1
|
|
|
|
|
$ST $lo1,0($tp) ; tp[j-1]
|
|
|
|
|
|
|
|
|
|
addic $ovf,$ovf,-1 ; move upmost overflow to XER[CA]
|
|
|
|
|
li $ovf,0
|
|
|
|
|
adde $hi1,$hi1,$hi0
|
|
|
|
|
addze $ovf,$ovf
|
|
|
|
|
$ST $hi1,$BNSZ($tp)
|
|
|
|
|
;
|
|
|
|
|
slwi $tj,$num,`log($BNSZ)/log(2)`
|
|
|
|
|
$UCMP $i,$tj
|
|
|
|
|
addi $i,$i,$BNSZ
|
2016-03-31 16:47:17 +00:00
|
|
|
|
ble Louter
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
|
|
|
|
addi $num,$num,2 ; restore $num
|
2007-06-17 17:10:03 +00:00
|
|
|
|
subfc $j,$j,$j ; j=0 and "clear" XER[CA]
|
2011-05-27 13:32:34 +00:00
|
|
|
|
addi $tp,$sp,$LOCALS
|
2006-04-30 21:15:29 +00:00
|
|
|
|
mtctr $num
|
2007-06-17 17:10:03 +00:00
|
|
|
|
|
|
|
|
|
.align 4
|
|
|
|
|
Lsub: $LDX $tj,$tp,$j
|
|
|
|
|
$LDX $nj,$np,$j
|
|
|
|
|
subfe $aj,$nj,$tj ; tp[j]-np[j]
|
|
|
|
|
$STX $aj,$rp,$j
|
|
|
|
|
addi $j,$j,$BNSZ
|
2016-03-31 16:47:17 +00:00
|
|
|
|
bdnz Lsub
|
2007-06-17 17:10:03 +00:00
|
|
|
|
|
2006-04-30 21:15:29 +00:00
|
|
|
|
li $j,0
|
2007-06-17 17:10:03 +00:00
|
|
|
|
mtctr $num
|
|
|
|
|
subfe $ovf,$j,$ovf ; handle upmost overflow bit
|
|
|
|
|
and $ap,$tp,$ovf
|
|
|
|
|
andc $np,$rp,$ovf
|
|
|
|
|
or $ap,$ap,$np ; ap=borrow?tp:rp
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
|
|
|
|
.align 4
|
2007-06-17 17:10:03 +00:00
|
|
|
|
Lcopy: ; copy or in-place refresh
|
|
|
|
|
$LDX $tj,$ap,$j
|
2006-04-30 21:15:29 +00:00
|
|
|
|
$STX $tj,$rp,$j
|
|
|
|
|
$STX $j,$tp,$j ; zap at once
|
|
|
|
|
addi $j,$j,$BNSZ
|
2016-03-31 16:47:17 +00:00
|
|
|
|
bdnz Lcopy
|
2006-04-30 21:15:29 +00:00
|
|
|
|
|
2011-05-27 13:32:34 +00:00
|
|
|
|
$POP $tj,0($sp)
|
2006-04-30 21:15:29 +00:00
|
|
|
|
li r3,1
|
2011-05-27 13:32:34 +00:00
|
|
|
|
$POP r20,`-12*$SIZE_T`($tj)
|
|
|
|
|
$POP r21,`-11*$SIZE_T`($tj)
|
|
|
|
|
$POP r22,`-10*$SIZE_T`($tj)
|
|
|
|
|
$POP r23,`-9*$SIZE_T`($tj)
|
|
|
|
|
$POP r24,`-8*$SIZE_T`($tj)
|
|
|
|
|
$POP r25,`-7*$SIZE_T`($tj)
|
|
|
|
|
$POP r26,`-6*$SIZE_T`($tj)
|
|
|
|
|
$POP r27,`-5*$SIZE_T`($tj)
|
|
|
|
|
$POP r28,`-4*$SIZE_T`($tj)
|
|
|
|
|
$POP r29,`-3*$SIZE_T`($tj)
|
|
|
|
|
$POP r30,`-2*$SIZE_T`($tj)
|
|
|
|
|
$POP r31,`-1*$SIZE_T`($tj)
|
|
|
|
|
mr $sp,$tj
|
2006-04-30 21:15:29 +00:00
|
|
|
|
blr
|
|
|
|
|
.long 0
|
2011-05-27 13:32:34 +00:00
|
|
|
|
.byte 0,12,4,0,0x80,12,6,0
|
|
|
|
|
.long 0
|
2013-10-14 22:14:39 +00:00
|
|
|
|
.size .bn_mul_mont_int,.-.bn_mul_mont_int
|
2011-05-27 13:32:34 +00:00
|
|
|
|
|
|
|
|
|
.asciz "Montgomery Multiplication for PPC, CRYPTOGAMS by <appro\@openssl.org>"
|
2006-04-30 21:15:29 +00:00
|
|
|
|
___
|
|
|
|
|
|
|
|
|
|
$code =~ s/\`([^\`]*)\`/eval $1/gem;
|
|
|
|
|
print $code;
|
|
|
|
|
close STDOUT;
|