3
# ====================================================================
4
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5
# project. The module is, however, dual licensed under OpenSSL and
6
# CRYPTOGAMS licenses depending on where you obtain it. For further
7
# details see http://www.openssl.org/~appro/cryptogams/.
8
# ====================================================================
12
# Montgomery multiplication for ARMv4.
14
# Performance improvement naturally varies among CPU implementations
15
# and compilers. The code was observed to provide +65-35% improvement
16
# [depending on key length, less for longer keys] on ARM920T, and
17
# +115-80% on Intel IXP425. This is compared to pre-bn_mul_mont code
18
# base and compiler generated code with in-lined umull and even umlal
19
# instructions. The latter means that this code didn't really have an
20
# "advantage" of utilizing some "secret" instruction.
22
# The code is interoperable with Thumb ISA and is rather compact, less
23
# than 1/2KB. Windows CE port would be trivial, as it's exclusively
24
# about decorations, ABI and instruction syntax are identical.
26
$num="r0"; # starts as num argument, but holds &tp[num-1]
28
$bp="r2"; $bi="r2"; $rp="r2";
35
########### # r9 is reserved by ELF as platform specific, e.g. TLS pointer
36
$alo="r10"; # sl, gcc uses it to keep @GOT
39
########### # r13 is stack pointer
41
########### # r15 is program counter
43
#### argument block layout relative to &tp[num-1], a.k.a. $num
45
# ap permanently resides in r1
47
# np permanently resides in r3
49
$_num="$num,#15*4"; $_bpend=$_num;
55
.type bn_mul_mont,%function
59
stmdb sp!,{r0,r2} @ sp points at argument block
60
ldr $num,[sp,#3*4] @ load num
66
stmdb sp!,{r4-r12,lr} @ save 10 registers
68
mov $num,$num,lsl#2 @ rescale $num for byte count
69
sub sp,sp,$num @ alloca(4*num)
70
sub sp,sp,#4 @ +extra dword
71
sub $num,$num,#4 @ "num=num-1"
72
add $tp,$bp,$num @ &bp[num-1]
74
add $num,sp,$num @ $num to point at &tp[num-1]
77
ldr $aj,[$ap],#4 @ ap[0],ap++
78
ldr $nj,[$np],#4 @ np[0],np++
80
str $tp,[$_bpend] @ save &bp[num]
82
umull $alo,$ahi,$aj,$bi @ ap[0]*bp[0]
83
str $n0,[$_n0] @ save n0 value
84
mul $n0,$alo,$n0 @ "tp[0]"*n0
86
umlal $alo,$nlo,$nj,$n0 @ np[0]*n0+"t[0]"
90
ldr $aj,[$ap],#4 @ ap[j],ap++
93
umlal $alo,$ahi,$aj,$bi @ ap[j]*bp[0]
94
ldr $nj,[$np],#4 @ np[j],np++
96
umlal $nlo,$nhi,$nj,$n0 @ np[j]*n0
98
str $nlo,[$tp],#4 @ tp[j-1]=,tp++
106
ldr $tp,[$_bp] @ restore bp
107
str $nlo,[$num] @ tp[num-1]=
108
ldr $n0,[$_n0] @ restore n0
109
str $nhi,[$num,#4] @ tp[num]=
112
sub $tj,$num,sp @ "original" $num-1 value
113
sub $ap,$ap,$tj @ "rewind" ap to &ap[1]
114
sub $np,$np,$tj @ "rewind" np to &np[1]
115
ldr $bi,[$tp,#4]! @ *(++bp)
116
ldr $aj,[$ap,#-4] @ ap[0]
117
ldr $nj,[$np,#-4] @ np[0]
118
ldr $alo,[sp] @ tp[0]
119
ldr $tj,[sp,#4] @ tp[1]
122
umlal $alo,$ahi,$aj,$bi @ ap[0]*bp[i]+tp[0]
123
str $tp,[$_bp] @ save bp
126
umlal $alo,$nlo,$nj,$n0 @ np[0]*n0+"tp[0]"
130
ldr $aj,[$ap],#4 @ ap[j],ap++
131
adds $alo,$ahi,$tj @ +=tp[j]
133
umlal $alo,$ahi,$aj,$bi @ ap[j]*bp[i]
134
ldr $nj,[$np],#4 @ np[j],np++
136
umlal $nlo,$nhi,$nj,$n0 @ np[j]*n0
137
ldr $tj,[$tp,#8] @ tp[j+1]
140
str $nlo,[$tp],#4 @ tp[j-1]=,tp++
150
ldr $tp,[$_bp] @ restore bp
151
ldr $tj,[$_bpend] @ restore &bp[num]
152
str $nlo,[$num] @ tp[num-1]=
153
ldr $n0,[$_n0] @ restore n0
154
str $nhi,[$num,#4] @ tp[num]=
159
ldr $rp,[$_rp] @ pull rp
160
add $num,$num,#4 @ $num to point at &tp[num]
161
sub $aj,$num,sp @ "original" num value
162
mov $tp,sp @ "rewind" $tp
163
mov $ap,$tp @ "borrow" $ap
164
sub $np,$np,$aj @ "rewind" $np to &np[0]
166
subs $tj,$tj,$tj @ "clear" carry flag
167
.Lsub: ldr $tj,[$tp],#4
169
sbcs $tj,$tj,$nj @ tp[j]-np[j]
170
str $tj,[$rp],#4 @ rp[j]=
171
teq $tp,$num @ preserve carry
173
sbcs $nhi,$nhi,#0 @ upmost carry
174
mov $tp,sp @ "rewind" $tp
175
sub $rp,$rp,$aj @ "rewind" $rp
179
orr $ap,$ap,$np @ ap=borrow?tp:rp
181
.Lcopy: ldr $tj,[$ap],#4 @ copy or in-place refresh
182
str sp,[$tp],#4 @ zap tp
187
add sp,$num,#4 @ skip over tp[num+1]
188
ldmia sp!,{r4-r12,lr} @ restore registers
189
add sp,sp,#2*4 @ skip over {r0,r2}
192
moveq pc,lr @ be binary compatible with V4, yet
193
bx lr @ interoperable with Thumb ISA:-)
194
.size bn_mul_mont,.-bn_mul_mont
195
.asciz "Montgomery multiplication for ARMv4, CRYPTOGAMS by <appro\@openssl.org>"
199
$code =~ s/\bbx\s+lr\b/.word\t0xe12fff1e/gm; # make it possible to compile with -march=armv4