]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/asm/x86-mont.pl
32daf5c215096ce2521e525da115ef791aa50b88
2 # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
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/.
15 # ====================================================================
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%:-)
32 # Modulo-scheduling SSE2 loops results in further 15-20% improvement.
33 # Integer-only code [being equipped with dedicated squaring procedure]
34 # gives ~40% on rsa512 sign benchmark...
36 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
37 push(@INC,"${dir}","${dir}../../perlasm");
41 open STDOUT
,">$output";
46 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
48 &external_label
("OPENSSL_ia32cap_P") if ($sse2);
50 &function_begin
("bn_mul_mont");
54 $ap="esi"; $tp="esi"; # overlapping variables!!!
55 $rp="edi"; $bp="edi"; # overlapping variables!!!
59 $_num=&DWP
(4*0,"esp"); # stack top layout
64 $_n0=&DWP
(4*5,"esp"); $_n0q=&QWP
(4*5,"esp");
66 $_bpend=&DWP
(4*7,"esp");
67 $frame=32; # size of above frame rounded up to 16n
70 &mov
("edi",&wparam
(5)); # int num
72 &jl
(&label
("just_leave"));
74 &lea
("esi",&wparam
(0)); # put aside pointer to argument block
75 &lea
("edx",&wparam
(1)); # load ap
76 &add
("edi",2); # extra two words on top of tp
78 &lea
("ebp",&DWP
(-$frame,"esp","edi",4)); # future alloca($frame+4*(num+2))
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
88 &sub ("ebp","eax"); # this aligns sp and ap modulo 2048
93 &sub ("ebp","edx"); # this splits them apart modulo 4096
95 &and ("ebp",-64); # align to cache line
97 # An OS-agnostic version of __chkstk.
99 # Some OSes (Windows) insist on stack being "wired" to
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...
108 &mov
("edx","esp"); # saved stack pointer!
109 &lea
("esp",&DWP
(0,"ebp","eax"));
110 &mov
("eax",&DWP
(0,"esp"));
112 &ja
(&label
("page_walk"));
113 &jmp
(&label
("page_walk_done"));
115 &set_label
("page_walk",16);
116 &lea
("esp",&DWP
(-4096,"esp"));
117 &mov
("eax",&DWP
(0,"esp"));
119 &ja
(&label
("page_walk"));
120 &set_label
("page_walk_done");
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
126 &mov
("ebp",&DWP
(3*4,"esi"));# const BN_ULONG *np
127 &mov
("esi",&DWP
(4*4,"esi"));# const BN_ULONG *n0
128 #&mov ("edi",&DWP(5*4,"esi"));# int num
130 &mov
("esi",&DWP
(0,"esi")); # pull n0[0]
131 &mov
($_rp,"eax"); # ... save a copy of argument block
136 &lea
($num,&DWP
(-3,"edi")); # num=num-1 to assist modulo-scheduling
137 #&mov ($_num,$num); # redundant as $num is not reused
138 &mov
($_sp,"edx"); # saved stack pointer!
141 $acc0="mm0"; # mmx register bank layout
150 &picmeup
("eax","OPENSSL_ia32cap_P");
151 &bt
(&DWP
(0,"eax"),26);
152 &jnc
(&label
("non_sse2"));
155 &movd
($mask,"eax"); # mask 32 lower bits
157 &mov
($ap,$_ap); # load input pointers
164 &movd
($mul0,&DWP
(0,$bp)); # bp[0]
165 &movd
($mul1,&DWP
(0,$ap)); # ap[0]
166 &movd
($car1,&DWP
(0,$np)); # np[0]
168 &pmuludq
($mul1,$mul0); # ap[0]*bp[0]
170 &movq
($acc0,$mul1); # I wish movd worked for
171 &pand
($acc0,$mask); # inter-register transfers
173 &pmuludq
($mul1,$_n0q); # *=n0
175 &pmuludq
($car1,$mul1); # "t[0]"*np[0]*n0
176 &paddq
($car1,$acc0);
178 &movd
($acc1,&DWP
(4,$np)); # np[1]
179 &movd
($acc0,&DWP
(4,$ap)); # ap[1]
185 &set_label
("1st",16);
186 &pmuludq
($acc0,$mul0); # ap[j]*bp[0]
187 &pmuludq
($acc1,$mul1); # np[j]*m1
188 &paddq
($car0,$acc0); # +=c0
189 &paddq
($car1,$acc1); # +=c1
193 &movd
($acc1,&DWP
(4,$np,$j,4)); # np[j+1]
194 &paddq
($car1,$acc0); # +=ap[j]*bp[0];
195 &movd
($acc0,&DWP
(4,$ap,$j,4)); # ap[j+1]
197 &movd
(&DWP
($frame-4,"esp",$j,4),$car1); # tp[j-1]=
200 &lea
($j,&DWP
(1,$j));
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
211 &paddq
($car1,$acc0); # +=ap[num-1]*bp[0];
212 &movd
(&DWP
($frame-4,"esp",$j,4),$car1); # tp[num-2]=
217 &paddq
($car1,$car0);
218 &movq
(&QWP
($frame,"esp",$num,4),$car1); # tp[num].tp[num-1]
224 &movd
($mul0,&DWP
(0,$bp,$i,4)); # bp[i]
225 &movd
($mul1,&DWP
(0,$ap)); # ap[0]
226 &movd
($temp,&DWP
($frame,"esp")); # tp[0]
227 &movd
($car1,&DWP
(0,$np)); # np[0]
228 &pmuludq
($mul1,$mul0); # ap[0]*bp[i]
230 &paddq
($mul1,$temp); # +=tp[0]
235 &pmuludq
($mul1,$_n0q); # *=n0
237 &pmuludq
($car1,$mul1);
238 &paddq
($car1,$acc0);
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]
246 &paddq
($car0,$temp); # +=tp[1]
251 &pmuludq
($acc0,$mul0); # ap[j]*bp[i]
252 &pmuludq
($acc1,$mul1); # np[j]*m1
253 &paddq
($car0,$acc0); # +=c0
254 &paddq
($car1,$acc1); # +=c1
257 &movd
($temp,&DWP
($frame+4,"esp",$j,4));# tp[j+1]
259 &movd
($acc1,&DWP
(4,$np,$j,4)); # np[j+1]
260 &paddq
($car1,$acc0); # +=ap[j]*bp[i]+tp[j]
261 &movd
($acc0,&DWP
(4,$ap,$j,4)); # ap[j+1]
263 &movd
(&DWP
($frame-4,"esp",$j,4),$car1);# tp[j-1]=
265 &paddq
($car0,$temp); # +=tp[j+1]
268 &lea
($j,&DWP
(1,$j)); # j++
269 &jnz
(&label
("inner"));
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
279 &paddq
($car1,$acc0); # +=ap[num-1]*bp[i]+tp[num-1]
280 &movd
(&DWP
($frame-4,"esp",$j,4),$car1); # tp[num-2]=
284 &movd
($temp,&DWP
($frame+4,"esp",$num,4)); # += tp[num]
285 &paddq
($car1,$car0);
286 &paddq
($car1,$temp);
287 &movq
(&QWP
($frame,"esp",$num,4),$car1); # tp[num].tp[num-1]
289 &lea
($i,&DWP
(1,$i)); # i++
291 &jle
(&label
("outer"));
293 &emms
(); # done with mmx bank
294 &jmp
(&label
("common_tail"));
296 &set_label
("non_sse2",16);
301 &xor ("eax","eax"); # signal "not fast enough [yet]"
302 &jmp
(&label
("just_leave"));
303 # While the below code provides competitive performance for
304 # all key lengths on modern Intel cores, it's still more
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...
310 $inp="esi"; # integer path uses these registers differently
315 &lea
($carry,&DWP
(1,$num));
319 &and ($carry,1); # see if num is even
320 &sub ("edx",$word); # see if ap==bp
321 &lea
("eax",&DWP
(4,$word,$num,4)); # &bp[num]
323 &mov
($word,&DWP
(0,$word)); # bp[0]
324 &jz
(&label
("bn_sqr_mont"));
325 &mov
($_bpend,"eax");
326 &mov
("eax",&DWP
(0,$inp));
329 &set_label
("mull",16);
331 &mul
($word); # ap[j]*bp[0]
333 &lea
($j,&DWP
(1,$j));
335 &mov
("eax",&DWP
(0,$inp,$j,4)); # ap[j+1]
337 &mov
(&DWP
($frame-4,"esp",$j,4),$carry); # tp[j]=
338 &jl
(&label
("mull"));
341 &mul
($word); # ap[num-1]*bp[0]
346 &imul
($word,&DWP
($frame,"esp")); # n0*tp[0]
348 &mov
(&DWP
($frame,"esp",$num,4),"eax"); # tp[num-1]=
350 &mov
(&DWP
($frame+4,"esp",$num,4),"edx"); # tp[num]=
351 &mov
(&DWP
($frame+8,"esp",$num,4),$j); # tp[num+1]=
353 &mov
("eax",&DWP
(0,$inp)); # np[0]
354 &mul
($word); # np[0]*m
355 &add
("eax",&DWP
($frame,"esp")); # +=tp[0]
356 &mov
("eax",&DWP
(4,$inp)); # np[1]
360 &jmp
(&label
("2ndmadd"));
362 &set_label
("1stmadd",16);
364 &mul
($word); # ap[j]*bp[i]
365 &add
($carry,&DWP
($frame,"esp",$j,4)); # +=tp[j]
366 &lea
($j,&DWP
(1,$j));
369 &mov
("eax",&DWP
(0,$inp,$j,4)); # ap[j+1]
372 &mov
(&DWP
($frame-4,"esp",$j,4),$carry); # tp[j]=
373 &jl
(&label
("1stmadd"));
376 &mul
($word); # ap[num-1]*bp[i]
377 &add
("eax",&DWP
($frame,"esp",$num,4)); # +=tp[num-1]
383 &imul
($word,&DWP
($frame,"esp")); # n0*tp[0]
386 &add
("edx",&DWP
($frame+4,"esp",$num,4)); # carry+=tp[num]
387 &mov
(&DWP
($frame,"esp",$num,4),$carry); # tp[num-1]=
389 &mov
("eax",&DWP
(0,$inp)); # np[0]
390 &mov
(&DWP
($frame+4,"esp",$num,4),"edx"); # tp[num]=
391 &mov
(&DWP
($frame+8,"esp",$num,4),$j); # tp[num+1]=
393 &mul
($word); # np[0]*m
394 &add
("eax",&DWP
($frame,"esp")); # +=tp[0]
395 &mov
("eax",&DWP
(4,$inp)); # np[1]
399 &set_label
("2ndmadd",16);
401 &mul
($word); # np[j]*m
402 &add
($carry,&DWP
($frame,"esp",$j,4)); # +=tp[j]
403 &lea
($j,&DWP
(1,$j));
406 &mov
("eax",&DWP
(0,$inp,$j,4)); # np[j+1]
409 &mov
(&DWP
($frame-8,"esp",$j,4),$carry); # tp[j-1]=
410 &jl
(&label
("2ndmadd"));
413 &mul
($word); # np[j]*m
414 &add
($carry,&DWP
($frame,"esp",$num,4)); # +=tp[num-1]
418 &mov
(&DWP
($frame-4,"esp",$num,4),$carry); # tp[num-2]=
421 &mov
($j,$_bp); # &bp[i]
422 &add
("edx",&DWP
($frame+4,"esp",$num,4)); # carry+=tp[num]
423 &adc
("eax",&DWP
($frame+8,"esp",$num,4)); # +=tp[num+1]
424 &lea
($j,&DWP
(4,$j));
425 &mov
(&DWP
($frame,"esp",$num,4),"edx"); # tp[num-1]=
427 &mov
(&DWP
($frame+4,"esp",$num,4),"eax"); # tp[num]=
428 &je
(&label
("common_tail"));
430 &mov
($word,&DWP
(0,$j)); # bp[i+1]
432 &mov
($_bp,$j); # &bp[++i]
435 &mov
("eax",&DWP
(0,$inp));
436 &jmp
(&label
("1stmadd"));
438 &set_label
("bn_sqr_mont",16);
441 &mov
($_bp,$j); # i=0
443 &mov
("eax",$word); # ap[0]
444 &mul
($word); # ap[0]*ap[0]
445 &mov
(&DWP
($frame,"esp"),"eax"); # tp[0]=
450 &set_label
("sqr",16);
451 &mov
("eax",&DWP
(0,$inp,$j,4)); # ap[j]
453 &mul
($word); # ap[j]*ap[0]
455 &lea
($j,&DWP
(1,$j));
457 &lea
($carry,&DWP
(0,$sbit,"eax",2));
461 &mov
(&DWP
($frame-4,"esp",$j,4),$carry); # tp[j]=
464 &mov
("eax",&DWP
(0,$inp,$j,4)); # ap[num-1]
466 &mul
($word); # ap[num-1]*ap[0]
471 &lea
($carry,&DWP
(0,$sbit,"eax",2));
472 &imul
($word,&DWP
($frame,"esp")); # n0*tp[0]
474 &mov
(&DWP
($frame,"esp",$j,4),$carry); # tp[num-1]=
476 &lea
($carry,&DWP
(0,"eax","edx",2));
477 &mov
("eax",&DWP
(0,$inp)); # np[0]
479 &mov
(&DWP
($frame+4,"esp",$j,4),$carry); # tp[num]=
480 &mov
(&DWP
($frame+8,"esp",$j,4),"edx"); # tp[num+1]=
482 &mul
($word); # np[0]*m
483 &add
("eax",&DWP
($frame,"esp")); # +=tp[0]
486 &mov
("eax",&DWP
(4,$inp)); # np[1]
489 &set_label
("3rdmadd",16);
491 &mul
($word); # np[j]*m
492 &add
($carry,&DWP
($frame,"esp",$j,4)); # +=tp[j]
495 &mov
("eax",&DWP
(4,$inp,$j,4)); # np[j+1]
497 &mov
(&DWP
($frame-4,"esp",$j,4),$carry); # tp[j-1]=
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));
505 &mov
("eax",&DWP
(0,$inp,$j,4)); # np[j+2]
508 &mov
(&DWP
($frame-8,"esp",$j,4),$carry); # tp[j]=
509 &jl
(&label
("3rdmadd"));
512 &mul
($word); # np[j]*m
513 &add
($carry,&DWP
($frame,"esp",$num,4)); # +=tp[num-1]
517 &mov
(&DWP
($frame-4,"esp",$num,4),$carry); # tp[num-2]=
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]=
526 &mov
(&DWP
($frame+4,"esp",$num,4),"eax"); # tp[num]=
527 &je
(&label
("common_tail"));
529 &mov
($word,&DWP
(4,$inp,$j,4)); # ap[i]
530 &lea
($j,&DWP
(1,$j));
532 &mov
($_bp,$j); # ++i
533 &mul
($word); # ap[i]*ap[i]
534 &add
("eax",&DWP
($frame,"esp",$j,4)); # +=tp[i]
536 &mov
(&DWP
($frame,"esp",$j,4),"eax"); # tp[i]=
537 &xor ($carry,$carry);
539 &lea
($j,&DWP
(1,$j));
540 &je
(&label
("sqrlast"));
542 &mov
($sbit,"edx"); # zaps $num
545 &set_label
("sqradd",16);
546 &mov
("eax",&DWP
(0,$inp,$j,4)); # ap[j]
548 &mul
($word); # ap[j]*ap[i]
550 &lea
($carry,&DWP
(0,"eax","eax"));
553 &add
($carry,&DWP
($frame,"esp",$j,4)); # +=tp[j]
554 &lea
($j,&DWP
(1,$j));
559 &mov
(&DWP
($frame-4,"esp",$j,4),$carry); # tp[j]=
561 &jle
(&label
("sqradd"));
568 &set_label
("sqrlast");
571 &imul
($word,&DWP
($frame,"esp")); # n0*tp[0]
573 &add
("edx",&DWP
($frame,"esp",$j,4)); # +=tp[num]
574 &mov
("eax",&DWP
(0,$inp)); # np[0]
576 &mov
(&DWP
($frame,"esp",$j,4),"edx"); # tp[num]=
577 &mov
(&DWP
($frame+4,"esp",$j,4),$carry); # tp[num+1]=
579 &mul
($word); # np[0]*m
580 &add
("eax",&DWP
($frame,"esp")); # +=tp[0]
581 &lea
($num,&DWP
(-1,$j));
584 &mov
("eax",&DWP
(4,$inp)); # np[1]
586 &jmp
(&label
("3rdmadd"));
589 &set_label
("common_tail",16);
590 &mov
($np,$_np); # load modulus pointer
591 &mov
($rp,$_rp); # load result pointer
592 &lea
($tp,&DWP
($frame,"esp")); # [$ap and $bp are zapped]
594 &mov
("eax",&DWP
(0,$tp)); # tp[0]
595 &mov
($j,$num); # j=num-1
596 &xor ($i,$i); # i=0 and clear CF!
598 &set_label
("sub",16);
599 &sbb
("eax",&DWP
(0,$np,$i,4));
600 &mov
(&DWP
(0,$rp,$i,4),"eax"); # rp[i]=tp[i]-np[i]
601 &dec
($j); # doesn't affect CF!
602 &mov
("eax",&DWP
(4,$tp,$i,4)); # tp[i+1]
603 &lea
($i,&DWP
(1,$i)); # i++
604 &jge
(&label
("sub"));
606 &sbb
("eax",0); # handle upmost overflow bit
611 &or ($tp,$np); # tp=carry?tp:rp
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
618 &jge
(&label
("copy"));
620 &mov
("esp",$_sp); # pull saved stack pointer
622 &set_label
("just_leave");
623 &function_end
("bn_mul_mont");
625 &asciz
("Montgomery Multiplication for x86, CRYPTOGAMS by <appro\@openssl.org>");