]>
Commit | Line | Data |
---|---|---|
6aa36e8e RS |
1 | #! /usr/bin/env perl |
2 | # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | # | |
4 | # Licensed under the OpenSSL license (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 | |
8 | ||
54f3d200 AP |
9 | |
10 | # ==================================================================== | |
11 | # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL | |
48d2335d AP |
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/. | |
54f3d200 AP |
15 | # ==================================================================== |
16 | ||
35593b33 AP |
17 | # October 2005 |
18 | # | |
54f3d200 AP |
19 | # This is a "teaser" code, as it can be improved in several ways... |
20 | # First of all non-SSE2 path should be implemented (yes, for now it | |
21 | # performs Montgomery multiplication/convolution only on SSE2-capable | |
22 | # CPUs such as P4, others fall down to original code). Then inner loop | |
23 | # can be unrolled and modulo-scheduled to improve ILP and possibly | |
24 | # moved to 128-bit XMM register bank (though it would require input | |
25 | # rearrangement and/or increase bus bandwidth utilization). Dedicated | |
26 | # squaring procedure should give further performance improvement... | |
27 | # Yet, for being draft, the code improves rsa512 *sign* benchmark by | |
28 | # 110%(!), rsa1024 one - by 70% and rsa4096 - by 20%:-) | |
29 | ||
1702c8c4 AP |
30 | # December 2006 |
31 | # | |
32 | # Modulo-scheduling SSE2 loops results in further 15-20% improvement. | |
33 | # Integer-only code [being equipped with dedicated squaring procedure] | |
4cfe3df1 | 34 | # gives ~40% on rsa512 sign benchmark... |
1702c8c4 AP |
35 | |
36 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; | |
37 | push(@INC,"${dir}","${dir}../../perlasm"); | |
54f3d200 AP |
38 | require "x86asm.pl"; |
39 | ||
6bd7a4d9 RL |
40 | $output = pop; |
41 | open STDOUT,">$output"; | |
609b0852 | 42 | |
e195c8a2 | 43 | &asm_init($ARGV[0]); |
54f3d200 AP |
44 | |
45 | $sse2=0; | |
46 | for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); } | |
47 | ||
48 | &external_label("OPENSSL_ia32cap_P") if ($sse2); | |
49 | ||
1702c8c4 | 50 | &function_begin("bn_mul_mont"); |
54f3d200 | 51 | |
48d2335d | 52 | $i="edx"; |
54f3d200 | 53 | $j="ecx"; |
7d9cf7c0 | 54 | $ap="esi"; $tp="esi"; # overlapping variables!!! |
54f3d200 | 55 | $rp="edi"; $bp="edi"; # overlapping variables!!! |
48d2335d AP |
56 | $np="ebp"; |
57 | $num="ebx"; | |
54f3d200 | 58 | |
4cfe3df1 AP |
59 | $_num=&DWP(4*0,"esp"); # stack top layout |
60 | $_rp=&DWP(4*1,"esp"); | |
61 | $_ap=&DWP(4*2,"esp"); | |
62 | $_bp=&DWP(4*3,"esp"); | |
63 | $_np=&DWP(4*4,"esp"); | |
8b71d354 | 64 | $_n0=&DWP(4*5,"esp"); $_n0q=&QWP(4*5,"esp"); |
34736de4 | 65 | $_sp=&DWP(4*6,"esp"); |
48d2335d | 66 | $_bpend=&DWP(4*7,"esp"); |
34736de4 | 67 | $frame=32; # size of above frame rounded up to 16n |
54f3d200 | 68 | |
48d2335d AP |
69 | &xor ("eax","eax"); |
70 | &mov ("edi",&wparam(5)); # int num | |
1702c8c4 AP |
71 | &cmp ("edi",4); |
72 | &jl (&label("just_leave")); | |
54f3d200 | 73 | |
4cfe3df1 AP |
74 | &lea ("esi",&wparam(0)); # put aside pointer to argument block |
75 | &lea ("edx",&wparam(1)); # load ap | |
48d2335d AP |
76 | &add ("edi",2); # extra two words on top of tp |
77 | &neg ("edi"); | |
3ba1ef82 | 78 | &lea ("ebp",&DWP(-$frame,"esp","edi",4)); # future alloca($frame+4*(num+2)) |
48d2335d | 79 | &neg ("edi"); |
4cfe3df1 AP |
80 | |
81 | # minimize cache contention by arraning 2K window between stack | |
82 | # pointer and ap argument [np is also position sensitive vector, | |
83 | # but it's assumed to be near ap, as it's allocated at ~same | |
84 | # time]. | |
3ba1ef82 | 85 | &mov ("eax","ebp"); |
4cfe3df1 AP |
86 | &sub ("eax","edx"); |
87 | &and ("eax",2047); | |
3ba1ef82 | 88 | &sub ("ebp","eax"); # this aligns sp and ap modulo 2048 |
4cfe3df1 | 89 | |
3ba1ef82 | 90 | &xor ("edx","ebp"); |
4cfe3df1 AP |
91 | &and ("edx",2048); |
92 | &xor ("edx",2048); | |
3ba1ef82 | 93 | &sub ("ebp","edx"); # this splits them apart modulo 4096 |
4cfe3df1 | 94 | |
3ba1ef82 | 95 | &and ("ebp",-64); # align to cache line |
4cfe3df1 | 96 | |
0a86f668 EK |
97 | # An OS-agnostic version of __chkstk. |
98 | # | |
1bf80d93 | 99 | # Some OSes (Windows) insist on stack being "wired" to |
adc4f1fc AP |
100 | # physical memory in strictly sequential manner, i.e. if stack |
101 | # allocation spans two pages, then reference to farmost one can | |
102 | # be punishable by SEGV. But page walking can do good even on | |
103 | # other OSes, because it guarantees that villain thread hits | |
104 | # the guard page before it can make damage to innocent one... | |
3ba1ef82 AP |
105 | &mov ("eax","esp"); |
106 | &sub ("eax","ebp"); | |
adc4f1fc | 107 | &and ("eax",-4096); |
3ba1ef82 AP |
108 | &mov ("edx","esp"); # saved stack pointer! |
109 | &lea ("esp",&DWP(0,"ebp","eax")); | |
110 | &mov ("eax",&DWP(0,"esp")); | |
111 | &cmp ("esp","ebp"); | |
112 | &ja (&label("page_walk")); | |
113 | &jmp (&label("page_walk_done")); | |
114 | ||
115 | &set_label("page_walk",16); | |
116 | &lea ("esp",&DWP(-4096,"esp")); | |
117 | &mov ("eax",&DWP(0,"esp")); | |
118 | &cmp ("esp","ebp"); | |
119 | &ja (&label("page_walk")); | |
120 | &set_label("page_walk_done"); | |
adc4f1fc | 121 | |
4cfe3df1 AP |
122 | ################################# load argument block... |
123 | &mov ("eax",&DWP(0*4,"esi"));# BN_ULONG *rp | |
124 | &mov ("ebx",&DWP(1*4,"esi"));# const BN_ULONG *ap | |
125 | &mov ("ecx",&DWP(2*4,"esi"));# const BN_ULONG *bp | |
3ba1ef82 | 126 | &mov ("ebp",&DWP(3*4,"esi"));# const BN_ULONG *np |
4cfe3df1 AP |
127 | &mov ("esi",&DWP(4*4,"esi"));# const BN_ULONG *n0 |
128 | #&mov ("edi",&DWP(5*4,"esi"));# int num | |
54f3d200 | 129 | |
4d524040 | 130 | &mov ("esi",&DWP(0,"esi")); # pull n0[0] |
54f3d200 AP |
131 | &mov ($_rp,"eax"); # ... save a copy of argument block |
132 | &mov ($_ap,"ebx"); | |
133 | &mov ($_bp,"ecx"); | |
3ba1ef82 | 134 | &mov ($_np,"ebp"); |
54f3d200 | 135 | &mov ($_n0,"esi"); |
1702c8c4 | 136 | &lea ($num,&DWP(-3,"edi")); # num=num-1 to assist modulo-scheduling |
48d2335d | 137 | #&mov ($_num,$num); # redundant as $num is not reused |
3ba1ef82 | 138 | &mov ($_sp,"edx"); # saved stack pointer! |
1702c8c4 | 139 | \f |
48d2335d AP |
140 | if($sse2) { |
141 | $acc0="mm0"; # mmx register bank layout | |
142 | $acc1="mm1"; | |
143 | $car0="mm2"; | |
144 | $car1="mm3"; | |
145 | $mul0="mm4"; | |
146 | $mul1="mm5"; | |
147 | $temp="mm6"; | |
148 | $mask="mm7"; | |
149 | ||
150 | &picmeup("eax","OPENSSL_ia32cap_P"); | |
151 | &bt (&DWP(0,"eax"),26); | |
152 | &jnc (&label("non_sse2")); | |
54f3d200 AP |
153 | |
154 | &mov ("eax",-1); | |
155 | &movd ($mask,"eax"); # mask 32 lower bits | |
156 | ||
157 | &mov ($ap,$_ap); # load input pointers | |
158 | &mov ($bp,$_bp); | |
159 | &mov ($np,$_np); | |
160 | ||
161 | &xor ($i,$i); # i=0 | |
162 | &xor ($j,$j); # j=0 | |
163 | ||
164 | &movd ($mul0,&DWP(0,$bp)); # bp[0] | |
165 | &movd ($mul1,&DWP(0,$ap)); # ap[0] | |
166 | &movd ($car1,&DWP(0,$np)); # np[0] | |
167 | ||
168 | &pmuludq($mul1,$mul0); # ap[0]*bp[0] | |
169 | &movq ($car0,$mul1); | |
170 | &movq ($acc0,$mul1); # I wish movd worked for | |
171 | &pand ($acc0,$mask); # inter-register transfers | |
172 | ||
8b71d354 | 173 | &pmuludq($mul1,$_n0q); # *=n0 |
54f3d200 AP |
174 | |
175 | &pmuludq($car1,$mul1); # "t[0]"*np[0]*n0 | |
176 | &paddq ($car1,$acc0); | |
177 | ||
1702c8c4 AP |
178 | &movd ($acc1,&DWP(4,$np)); # np[1] |
179 | &movd ($acc0,&DWP(4,$ap)); # ap[1] | |
180 | ||
54f3d200 AP |
181 | &psrlq ($car0,32); |
182 | &psrlq ($car1,32); | |
183 | ||
184 | &inc ($j); # j++ | |
4cfe3df1 | 185 | &set_label("1st",16); |
54f3d200 AP |
186 | &pmuludq($acc0,$mul0); # ap[j]*bp[0] |
187 | &pmuludq($acc1,$mul1); # np[j]*m1 | |
54f3d200 | 188 | &paddq ($car0,$acc0); # +=c0 |
1702c8c4 AP |
189 | &paddq ($car1,$acc1); # +=c1 |
190 | ||
54f3d200 AP |
191 | &movq ($acc0,$car0); |
192 | &pand ($acc0,$mask); | |
1702c8c4 | 193 | &movd ($acc1,&DWP(4,$np,$j,4)); # np[j+1] |
54f3d200 | 194 | &paddq ($car1,$acc0); # +=ap[j]*bp[0]; |
1702c8c4 | 195 | &movd ($acc0,&DWP(4,$ap,$j,4)); # ap[j+1] |
54f3d200 | 196 | &psrlq ($car0,32); |
1702c8c4 | 197 | &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[j-1]= |
54f3d200 AP |
198 | &psrlq ($car1,32); |
199 | ||
200 | &lea ($j,&DWP(1,$j)); | |
201 | &cmp ($j,$num); | |
202 | &jl (&label("1st")); | |
203 | ||
1702c8c4 AP |
204 | &pmuludq($acc0,$mul0); # ap[num-1]*bp[0] |
205 | &pmuludq($acc1,$mul1); # np[num-1]*m1 | |
206 | &paddq ($car0,$acc0); # +=c0 | |
207 | &paddq ($car1,$acc1); # +=c1 | |
54f3d200 | 208 | |
1702c8c4 AP |
209 | &movq ($acc0,$car0); |
210 | &pand ($acc0,$mask); | |
211 | &paddq ($car1,$acc0); # +=ap[num-1]*bp[0]; | |
212 | &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[num-2]= | |
213 | ||
214 | &psrlq ($car0,32); | |
215 | &psrlq ($car1,32); | |
216 | ||
217 | &paddq ($car1,$car0); | |
8b71d354 | 218 | &movq (&QWP($frame,"esp",$num,4),$car1); # tp[num].tp[num-1] |
1702c8c4 | 219 | \f |
54f3d200 AP |
220 | &inc ($i); # i++ |
221 | &set_label("outer"); | |
222 | &xor ($j,$j); # j=0 | |
223 | ||
224 | &movd ($mul0,&DWP(0,$bp,$i,4)); # bp[i] | |
225 | &movd ($mul1,&DWP(0,$ap)); # ap[0] | |
34736de4 AP |
226 | &movd ($temp,&DWP($frame,"esp")); # tp[0] |
227 | &movd ($car1,&DWP(0,$np)); # np[0] | |
54f3d200 AP |
228 | &pmuludq($mul1,$mul0); # ap[0]*bp[i] |
229 | ||
230 | &paddq ($mul1,$temp); # +=tp[0] | |
231 | &movq ($acc0,$mul1); | |
232 | &movq ($car0,$mul1); | |
233 | &pand ($acc0,$mask); | |
234 | ||
8b71d354 | 235 | &pmuludq($mul1,$_n0q); # *=n0 |
54f3d200 AP |
236 | |
237 | &pmuludq($car1,$mul1); | |
238 | &paddq ($car1,$acc0); | |
239 | ||
1702c8c4 AP |
240 | &movd ($temp,&DWP($frame+4,"esp")); # tp[1] |
241 | &movd ($acc1,&DWP(4,$np)); # np[1] | |
242 | &movd ($acc0,&DWP(4,$ap)); # ap[1] | |
243 | ||
54f3d200 AP |
244 | &psrlq ($car0,32); |
245 | &psrlq ($car1,32); | |
1702c8c4 | 246 | &paddq ($car0,$temp); # +=tp[1] |
54f3d200 AP |
247 | |
248 | &inc ($j); # j++ | |
1702c8c4 | 249 | &dec ($num); |
54f3d200 | 250 | &set_label("inner"); |
54f3d200 AP |
251 | &pmuludq($acc0,$mul0); # ap[j]*bp[i] |
252 | &pmuludq($acc1,$mul1); # np[j]*m1 | |
54f3d200 | 253 | &paddq ($car0,$acc0); # +=c0 |
1702c8c4 AP |
254 | &paddq ($car1,$acc1); # +=c1 |
255 | ||
54f3d200 | 256 | &movq ($acc0,$car0); |
1702c8c4 | 257 | &movd ($temp,&DWP($frame+4,"esp",$j,4));# tp[j+1] |
54f3d200 | 258 | &pand ($acc0,$mask); |
1702c8c4 | 259 | &movd ($acc1,&DWP(4,$np,$j,4)); # np[j+1] |
54f3d200 | 260 | &paddq ($car1,$acc0); # +=ap[j]*bp[i]+tp[j] |
1702c8c4 | 261 | &movd ($acc0,&DWP(4,$ap,$j,4)); # ap[j+1] |
54f3d200 | 262 | &psrlq ($car0,32); |
1702c8c4 | 263 | &movd (&DWP($frame-4,"esp",$j,4),$car1);# tp[j-1]= |
54f3d200 | 264 | &psrlq ($car1,32); |
1702c8c4 | 265 | &paddq ($car0,$temp); # +=tp[j+1] |
54f3d200 | 266 | |
1702c8c4 | 267 | &dec ($num); |
54f3d200 | 268 | &lea ($j,&DWP(1,$j)); # j++ |
1702c8c4 AP |
269 | &jnz (&label("inner")); |
270 | ||
271 | &mov ($num,$j); | |
272 | &pmuludq($acc0,$mul0); # ap[num-1]*bp[i] | |
273 | &pmuludq($acc1,$mul1); # np[num-1]*m1 | |
274 | &paddq ($car0,$acc0); # +=c0 | |
275 | &paddq ($car1,$acc1); # +=c1 | |
276 | ||
277 | &movq ($acc0,$car0); | |
278 | &pand ($acc0,$mask); | |
279 | &paddq ($car1,$acc0); # +=ap[num-1]*bp[i]+tp[num-1] | |
280 | &movd (&DWP($frame-4,"esp",$j,4),$car1); # tp[num-2]= | |
281 | &psrlq ($car0,32); | |
282 | &psrlq ($car1,32); | |
54f3d200 | 283 | |
1702c8c4 | 284 | &movd ($temp,&DWP($frame+4,"esp",$num,4)); # += tp[num] |
54f3d200 AP |
285 | &paddq ($car1,$car0); |
286 | &paddq ($car1,$temp); | |
8b71d354 | 287 | &movq (&QWP($frame,"esp",$num,4),$car1); # tp[num].tp[num-1] |
54f3d200 AP |
288 | |
289 | &lea ($i,&DWP(1,$i)); # i++ | |
290 | &cmp ($i,$num); | |
1702c8c4 | 291 | &jle (&label("outer")); |
54f3d200 AP |
292 | |
293 | &emms (); # done with mmx bank | |
48d2335d AP |
294 | &jmp (&label("common_tail")); |
295 | ||
296 | &set_label("non_sse2",16); | |
297 | } | |
1702c8c4 AP |
298 | \f |
299 | if (0) { | |
48d2335d AP |
300 | &mov ("esp",$_sp); |
301 | &xor ("eax","eax"); # signal "not fast enough [yet]" | |
302 | &jmp (&label("just_leave")); | |
1702c8c4 | 303 | # While the below code provides competitive performance for |
60250017 | 304 | # all key lengths on modern Intel cores, it's still more |
4cfe3df1 AP |
305 | # than 10% slower for 4096-bit key elsewhere:-( "Competitive" |
306 | # means compared to the original integer-only assembler. | |
307 | # 512-bit RSA sign is better by ~40%, but that's about all | |
308 | # one can say about all CPUs... | |
48d2335d AP |
309 | } else { |
310 | $inp="esi"; # integer path uses these registers differently | |
311 | $word="edi"; | |
312 | $carry="ebp"; | |
313 | ||
48d2335d | 314 | &mov ($inp,$_ap); |
1702c8c4 | 315 | &lea ($carry,&DWP(1,$num)); |
48d2335d | 316 | &mov ($word,$_bp); |
1702c8c4 AP |
317 | &xor ($j,$j); # j=0 |
318 | &mov ("edx",$inp); | |
319 | &and ($carry,1); # see if num is even | |
320 | &sub ("edx",$word); # see if ap==bp | |
48d2335d | 321 | &lea ("eax",&DWP(4,$word,$num,4)); # &bp[num] |
1702c8c4 | 322 | &or ($carry,"edx"); |
48d2335d | 323 | &mov ($word,&DWP(0,$word)); # bp[0] |
1702c8c4 | 324 | &jz (&label("bn_sqr_mont")); |
48d2335d | 325 | &mov ($_bpend,"eax"); |
1702c8c4 | 326 | &mov ("eax",&DWP(0,$inp)); |
48d2335d AP |
327 | &xor ("edx","edx"); |
328 | ||
329 | &set_label("mull",16); | |
48d2335d AP |
330 | &mov ($carry,"edx"); |
331 | &mul ($word); # ap[j]*bp[0] | |
1702c8c4 | 332 | &add ($carry,"eax"); |
48d2335d | 333 | &lea ($j,&DWP(1,$j)); |
48d2335d | 334 | &adc ("edx",0); |
1702c8c4 | 335 | &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j+1] |
48d2335d | 336 | &cmp ($j,$num); |
1702c8c4 AP |
337 | &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]= |
338 | &jl (&label("mull")); | |
48d2335d | 339 | |
48d2335d AP |
340 | &mov ($carry,"edx"); |
341 | &mul ($word); # ap[num-1]*bp[0] | |
1702c8c4 | 342 | &mov ($word,$_n0); |
48d2335d | 343 | &add ("eax",$carry); |
1702c8c4 | 344 | &mov ($inp,$_np); |
48d2335d | 345 | &adc ("edx",0); |
1702c8c4 | 346 | &imul ($word,&DWP($frame,"esp")); # n0*tp[0] |
48d2335d AP |
347 | |
348 | &mov (&DWP($frame,"esp",$num,4),"eax"); # tp[num-1]= | |
349 | &xor ($j,$j); | |
350 | &mov (&DWP($frame+4,"esp",$num,4),"edx"); # tp[num]= | |
351 | &mov (&DWP($frame+8,"esp",$num,4),$j); # tp[num+1]= | |
352 | ||
353 | &mov ("eax",&DWP(0,$inp)); # np[0] | |
354 | &mul ($word); # np[0]*m | |
355 | &add ("eax",&DWP($frame,"esp")); # +=tp[0] | |
1702c8c4 | 356 | &mov ("eax",&DWP(4,$inp)); # np[1] |
48d2335d | 357 | &adc ("edx",0); |
1702c8c4 | 358 | &inc ($j); |
48d2335d AP |
359 | |
360 | &jmp (&label("2ndmadd")); | |
1702c8c4 | 361 | \f\f |
48d2335d | 362 | &set_label("1stmadd",16); |
48d2335d AP |
363 | &mov ($carry,"edx"); |
364 | &mul ($word); # ap[j]*bp[i] | |
1702c8c4 | 365 | &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j] |
48d2335d | 366 | &lea ($j,&DWP(1,$j)); |
48d2335d | 367 | &adc ("edx",0); |
1702c8c4 AP |
368 | &add ($carry,"eax"); |
369 | &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j+1] | |
48d2335d | 370 | &adc ("edx",0); |
48d2335d | 371 | &cmp ($j,$num); |
1702c8c4 AP |
372 | &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]= |
373 | &jl (&label("1stmadd")); | |
48d2335d | 374 | |
48d2335d AP |
375 | &mov ($carry,"edx"); |
376 | &mul ($word); # ap[num-1]*bp[i] | |
377 | &add ("eax",&DWP($frame,"esp",$num,4)); # +=tp[num-1] | |
1702c8c4 | 378 | &mov ($word,$_n0); |
48d2335d | 379 | &adc ("edx",0); |
1702c8c4 AP |
380 | &mov ($inp,$_np); |
381 | &add ($carry,"eax"); | |
48d2335d | 382 | &adc ("edx",0); |
1702c8c4 | 383 | &imul ($word,&DWP($frame,"esp")); # n0*tp[0] |
48d2335d AP |
384 | |
385 | &xor ($j,$j); | |
386 | &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num] | |
1702c8c4 | 387 | &mov (&DWP($frame,"esp",$num,4),$carry); # tp[num-1]= |
48d2335d | 388 | &adc ($j,0); |
1702c8c4 | 389 | &mov ("eax",&DWP(0,$inp)); # np[0] |
48d2335d AP |
390 | &mov (&DWP($frame+4,"esp",$num,4),"edx"); # tp[num]= |
391 | &mov (&DWP($frame+8,"esp",$num,4),$j); # tp[num+1]= | |
392 | ||
48d2335d AP |
393 | &mul ($word); # np[0]*m |
394 | &add ("eax",&DWP($frame,"esp")); # +=tp[0] | |
1702c8c4 | 395 | &mov ("eax",&DWP(4,$inp)); # np[1] |
48d2335d AP |
396 | &adc ("edx",0); |
397 | &mov ($j,1); | |
1702c8c4 | 398 | \f |
48d2335d | 399 | &set_label("2ndmadd",16); |
48d2335d AP |
400 | &mov ($carry,"edx"); |
401 | &mul ($word); # np[j]*m | |
1702c8c4 | 402 | &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j] |
48d2335d | 403 | &lea ($j,&DWP(1,$j)); |
48d2335d | 404 | &adc ("edx",0); |
1702c8c4 AP |
405 | &add ($carry,"eax"); |
406 | &mov ("eax",&DWP(0,$inp,$j,4)); # np[j+1] | |
48d2335d | 407 | &adc ("edx",0); |
48d2335d | 408 | &cmp ($j,$num); |
1702c8c4 AP |
409 | &mov (&DWP($frame-8,"esp",$j,4),$carry); # tp[j-1]= |
410 | &jl (&label("2ndmadd")); | |
48d2335d | 411 | |
48d2335d | 412 | &mov ($carry,"edx"); |
1702c8c4 | 413 | &mul ($word); # np[j]*m |
8f2d60ec | 414 | &add ($carry,&DWP($frame,"esp",$num,4)); # +=tp[num-1] |
48d2335d | 415 | &adc ("edx",0); |
1702c8c4 | 416 | &add ($carry,"eax"); |
48d2335d | 417 | &adc ("edx",0); |
1702c8c4 | 418 | &mov (&DWP($frame-4,"esp",$num,4),$carry); # tp[num-2]= |
48d2335d AP |
419 | |
420 | &xor ("eax","eax"); | |
1702c8c4 | 421 | &mov ($j,$_bp); # &bp[i] |
48d2335d AP |
422 | &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num] |
423 | &adc ("eax",&DWP($frame+8,"esp",$num,4)); # +=tp[num+1] | |
1702c8c4 | 424 | &lea ($j,&DWP(4,$j)); |
48d2335d | 425 | &mov (&DWP($frame,"esp",$num,4),"edx"); # tp[num-1]= |
1702c8c4 | 426 | &cmp ($j,$_bpend); |
48d2335d | 427 | &mov (&DWP($frame+4,"esp",$num,4),"eax"); # tp[num]= |
8f2d60ec | 428 | &je (&label("common_tail")); |
1702c8c4 | 429 | |
8f2d60ec | 430 | &mov ($word,&DWP(0,$j)); # bp[i+1] |
48d2335d | 431 | &mov ($inp,$_ap); |
1702c8c4 | 432 | &mov ($_bp,$j); # &bp[++i] |
48d2335d AP |
433 | &xor ($j,$j); |
434 | &xor ("edx","edx"); | |
1702c8c4 | 435 | &mov ("eax",&DWP(0,$inp)); |
48d2335d | 436 | &jmp (&label("1stmadd")); |
1702c8c4 AP |
437 | \f |
438 | &set_label("bn_sqr_mont",16); | |
439 | $sbit=$num; | |
440 | &mov ($_num,$num); | |
441 | &mov ($_bp,$j); # i=0 | |
442 | ||
443 | &mov ("eax",$word); # ap[0] | |
444 | &mul ($word); # ap[0]*ap[0] | |
445 | &mov (&DWP($frame,"esp"),"eax"); # tp[0]= | |
446 | &mov ($sbit,"edx"); | |
447 | &shr ("edx",1); | |
448 | &and ($sbit,1); | |
449 | &inc ($j); | |
450 | &set_label("sqr",16); | |
451 | &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j] | |
452 | &mov ($carry,"edx"); | |
453 | &mul ($word); # ap[j]*ap[0] | |
454 | &add ("eax",$carry); | |
455 | &lea ($j,&DWP(1,$j)); | |
456 | &adc ("edx",0); | |
457 | &lea ($carry,&DWP(0,$sbit,"eax",2)); | |
458 | &shr ("eax",31); | |
459 | &cmp ($j,$_num); | |
460 | &mov ($sbit,"eax"); | |
461 | &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]= | |
462 | &jl (&label("sqr")); | |
463 | ||
464 | &mov ("eax",&DWP(0,$inp,$j,4)); # ap[num-1] | |
465 | &mov ($carry,"edx"); | |
466 | &mul ($word); # ap[num-1]*ap[0] | |
467 | &add ("eax",$carry); | |
468 | &mov ($word,$_n0); | |
469 | &adc ("edx",0); | |
470 | &mov ($inp,$_np); | |
471 | &lea ($carry,&DWP(0,$sbit,"eax",2)); | |
472 | &imul ($word,&DWP($frame,"esp")); # n0*tp[0] | |
473 | &shr ("eax",31); | |
474 | &mov (&DWP($frame,"esp",$j,4),$carry); # tp[num-1]= | |
475 | ||
476 | &lea ($carry,&DWP(0,"eax","edx",2)); | |
477 | &mov ("eax",&DWP(0,$inp)); # np[0] | |
478 | &shr ("edx",31); | |
479 | &mov (&DWP($frame+4,"esp",$j,4),$carry); # tp[num]= | |
480 | &mov (&DWP($frame+8,"esp",$j,4),"edx"); # tp[num+1]= | |
481 | ||
482 | &mul ($word); # np[0]*m | |
483 | &add ("eax",&DWP($frame,"esp")); # +=tp[0] | |
484 | &mov ($num,$j); | |
485 | &adc ("edx",0); | |
486 | &mov ("eax",&DWP(4,$inp)); # np[1] | |
487 | &mov ($j,1); | |
488 | \f\f | |
489 | &set_label("3rdmadd",16); | |
490 | &mov ($carry,"edx"); | |
491 | &mul ($word); # np[j]*m | |
492 | &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j] | |
493 | &adc ("edx",0); | |
494 | &add ($carry,"eax"); | |
495 | &mov ("eax",&DWP(4,$inp,$j,4)); # np[j+1] | |
496 | &adc ("edx",0); | |
497 | &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j-1]= | |
498 | ||
499 | &mov ($carry,"edx"); | |
500 | &mul ($word); # np[j+1]*m | |
501 | &add ($carry,&DWP($frame+4,"esp",$j,4)); # +=tp[j+1] | |
502 | &lea ($j,&DWP(2,$j)); | |
503 | &adc ("edx",0); | |
504 | &add ($carry,"eax"); | |
505 | &mov ("eax",&DWP(0,$inp,$j,4)); # np[j+2] | |
506 | &adc ("edx",0); | |
507 | &cmp ($j,$num); | |
508 | &mov (&DWP($frame-8,"esp",$j,4),$carry); # tp[j]= | |
509 | &jl (&label("3rdmadd")); | |
510 | ||
511 | &mov ($carry,"edx"); | |
512 | &mul ($word); # np[j]*m | |
513 | &add ($carry,&DWP($frame,"esp",$num,4)); # +=tp[num-1] | |
514 | &adc ("edx",0); | |
515 | &add ($carry,"eax"); | |
516 | &adc ("edx",0); | |
517 | &mov (&DWP($frame-4,"esp",$num,4),$carry); # tp[num-2]= | |
518 | ||
519 | &mov ($j,$_bp); # i | |
520 | &xor ("eax","eax"); | |
521 | &mov ($inp,$_ap); | |
522 | &add ("edx",&DWP($frame+4,"esp",$num,4)); # carry+=tp[num] | |
523 | &adc ("eax",&DWP($frame+8,"esp",$num,4)); # +=tp[num+1] | |
524 | &mov (&DWP($frame,"esp",$num,4),"edx"); # tp[num-1]= | |
525 | &cmp ($j,$num); | |
526 | &mov (&DWP($frame+4,"esp",$num,4),"eax"); # tp[num]= | |
8f2d60ec | 527 | &je (&label("common_tail")); |
1702c8c4 AP |
528 | \f |
529 | &mov ($word,&DWP(4,$inp,$j,4)); # ap[i] | |
530 | &lea ($j,&DWP(1,$j)); | |
531 | &mov ("eax",$word); | |
532 | &mov ($_bp,$j); # ++i | |
533 | &mul ($word); # ap[i]*ap[i] | |
534 | &add ("eax",&DWP($frame,"esp",$j,4)); # +=tp[i] | |
535 | &adc ("edx",0); | |
536 | &mov (&DWP($frame,"esp",$j,4),"eax"); # tp[i]= | |
537 | &xor ($carry,$carry); | |
538 | &cmp ($j,$num); | |
539 | &lea ($j,&DWP(1,$j)); | |
540 | &je (&label("sqrlast")); | |
48d2335d | 541 | |
1702c8c4 AP |
542 | &mov ($sbit,"edx"); # zaps $num |
543 | &shr ("edx",1); | |
544 | &and ($sbit,1); | |
545 | &set_label("sqradd",16); | |
546 | &mov ("eax",&DWP(0,$inp,$j,4)); # ap[j] | |
547 | &mov ($carry,"edx"); | |
548 | &mul ($word); # ap[j]*ap[i] | |
549 | &add ("eax",$carry); | |
4cfe3df1 | 550 | &lea ($carry,&DWP(0,"eax","eax")); |
1702c8c4 | 551 | &adc ("edx",0); |
1702c8c4 | 552 | &shr ("eax",31); |
4cfe3df1 AP |
553 | &add ($carry,&DWP($frame,"esp",$j,4)); # +=tp[j] |
554 | &lea ($j,&DWP(1,$j)); | |
8f2d60ec | 555 | &adc ("eax",0); |
4cfe3df1 | 556 | &add ($carry,$sbit); |
1702c8c4 AP |
557 | &adc ("eax",0); |
558 | &cmp ($j,$_num); | |
559 | &mov (&DWP($frame-4,"esp",$j,4),$carry); # tp[j]= | |
560 | &mov ($sbit,"eax"); | |
561 | &jle (&label("sqradd")); | |
562 | ||
563 | &mov ($carry,"edx"); | |
5711dd8e | 564 | &add ("edx","edx"); |
1702c8c4 | 565 | &shr ($carry,31); |
5711dd8e AP |
566 | &add ("edx",$sbit); |
567 | &adc ($carry,0); | |
1702c8c4 AP |
568 | &set_label("sqrlast"); |
569 | &mov ($word,$_n0); | |
570 | &mov ($inp,$_np); | |
571 | &imul ($word,&DWP($frame,"esp")); # n0*tp[0] | |
572 | ||
573 | &add ("edx",&DWP($frame,"esp",$j,4)); # +=tp[num] | |
574 | &mov ("eax",&DWP(0,$inp)); # np[0] | |
575 | &adc ($carry,0); | |
576 | &mov (&DWP($frame,"esp",$j,4),"edx"); # tp[num]= | |
577 | &mov (&DWP($frame+4,"esp",$j,4),$carry); # tp[num+1]= | |
578 | ||
579 | &mul ($word); # np[0]*m | |
580 | &add ("eax",&DWP($frame,"esp")); # +=tp[0] | |
581 | &lea ($num,&DWP(-1,$j)); | |
582 | &adc ("edx",0); | |
583 | &mov ($j,1); | |
584 | &mov ("eax",&DWP(4,$inp)); # np[1] | |
585 | ||
586 | &jmp (&label("3rdmadd")); | |
48d2335d | 587 | } |
8f2d60ec | 588 | \f |
48d2335d | 589 | &set_label("common_tail",16); |
7d9cf7c0 | 590 | &mov ($np,$_np); # load modulus pointer |
54f3d200 | 591 | &mov ($rp,$_rp); # load result pointer |
7d9cf7c0 | 592 | &lea ($tp,&DWP($frame,"esp")); # [$ap and $bp are zapped] |
7d9cf7c0 AP |
593 | |
594 | &mov ("eax",&DWP(0,$tp)); # tp[0] | |
1702c8c4 | 595 | &mov ($j,$num); # j=num-1 |
7d9cf7c0 | 596 | &xor ($i,$i); # i=0 and clear CF! |
54f3d200 | 597 | |
48d2335d | 598 | &set_label("sub",16); |
54f3d200 AP |
599 | &sbb ("eax",&DWP(0,$np,$i,4)); |
600 | &mov (&DWP(0,$rp,$i,4),"eax"); # rp[i]=tp[i]-np[i] | |
54f3d200 | 601 | &dec ($j); # doesn't affect CF! |
7d9cf7c0 AP |
602 | &mov ("eax",&DWP(4,$tp,$i,4)); # tp[i+1] |
603 | &lea ($i,&DWP(1,$i)); # i++ | |
54f3d200 | 604 | &jge (&label("sub")); |
7d9cf7c0 AP |
605 | |
606 | &sbb ("eax",0); # handle upmost overflow bit | |
607 | &and ($tp,"eax"); | |
608 | ¬ ("eax"); | |
609 | &mov ($np,$rp); | |
610 | &and ($np,"eax"); | |
611 | &or ($tp,$np); # tp=carry?tp:rp | |
612 | ||
613 | &set_label("copy",16); # copy or in-place refresh | |
614 | &mov ("eax",&DWP(0,$tp,$num,4)); | |
615 | &mov (&DWP(0,$rp,$num,4),"eax"); # rp[i]=tp[i] | |
616 | &mov (&DWP($frame,"esp",$num,4),$j); # zap temporary vector | |
617 | &dec ($num); | |
618 | &jge (&label("copy")); | |
619 | ||
54f3d200 AP |
620 | &mov ("esp",$_sp); # pull saved stack pointer |
621 | &mov ("eax",1); | |
48d2335d | 622 | &set_label("just_leave"); |
54f3d200 AP |
623 | &function_end("bn_mul_mont"); |
624 | ||
1702c8c4 AP |
625 | &asciz("Montgomery Multiplication for x86, CRYPTOGAMS by <appro\@openssl.org>"); |
626 | ||
54f3d200 | 627 | &asm_finish(); |
6bd7a4d9 RL |
628 | |
629 | close STDOUT; |