3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> 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 # ====================================================================
10 # ECP_NISTZ256 module for x86/SSE2.
14 # Original ECP_NISTZ256 submission targeting x86_64 is detailed in
15 # http://eprint.iacr.org/2013/816. In the process of adaptation
16 # original .c module was made 32-bit savvy in order to make this
17 # implementation possible.
19 # with/without -DECP_NISTZ256_ASM
24 # Sandy Bridge +105-265% (contemporary i[57]-* are all close to this)
30 # Ranges denote minimum and maximum improvement coefficients depending
31 # on benchmark. Lower coefficients are for ECDSA sign, server-side
32 # operation. Keep in mind that +200% means 3x improvement.
34 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
35 push(@INC,"${dir}","${dir}../../perlasm");
38 &asm_init($ARGV[0],"ecp_nistz256-x86.pl",$ARGV[$#ARGV] eq "386");
41 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
43 &external_label("OPENSSL_ia32cap_P") if ($sse2);
46 ########################################################################
47 # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
49 open TABLE,"<ecp_nistz256_table.c" or
50 open TABLE,"<${dir}../ecp_nistz256_table.c" or
51 die "failed to open ecp_nistz256_table.c:",$!;
56 s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
60 # See ecp_nistz256_table.c for explanation for why it's 64*16*37.
61 # 64*16*37-1 is because $#arr returns last valid index or @arr, not
63 die "insane number of elements" if ($#arr != 64*16*37-1);
65 &public_label("ecp_nistz256_precomputed");
67 &set_label("ecp_nistz256_precomputed");
69 ########################################################################
70 # this conversion smashes P256_POINT_AFFINE by individual bytes with
71 # 64 byte interval, similar to
75 @tbl = splice(@arr,0,64*16);
76 for($i=0;$i<64;$i++) {
78 for($j=0;$j<64;$j++) {
79 push @line,(@tbl[$j*16+$i/4]>>(($i%4)*8))&0xff;
81 &data_byte(join(',',map { sprintf "0x%02x",$_} @line));
85 ########################################################################
86 # Keep in mind that constants are stored least to most significant word
89 &data_word(3,0,-1,-5,-2,-1,-3,4); # 2^512 mod P-256
91 &static_label("ONE_mont");
92 &set_label("ONE_mont");
93 &data_word(1,0,0,-1,-1,-1,-2,0);
97 &data_word(1,0,0,0,0,0,0,0);
98 &asciz("ECP_NISZ256 for x86/SSE2, CRYPTOGAMS by <appro\@openssl.org>");
101 ########################################################################
102 # void ecp_nistz256_mul_by_2(BN_ULONG edi[8],const BN_ULONG esi[8]);
103 &function_begin("ecp_nistz256_mul_by_2");
104 &mov ("esi",&wparam(1));
105 &mov ("edi",&wparam(0));
107 ########################################################################
108 # common pattern for internal functions is that %edi is result pointer,
109 # %esi and %ebp are input ones, %ebp being optional. %edi is preserved.
110 &call ("_ecp_nistz256_add");
111 &function_end("ecp_nistz256_mul_by_2");
113 ########################################################################
114 # void ecp_nistz256_mul_by_3(BN_ULONG edi[8],const BN_ULONG esi[8]);
115 &function_begin("ecp_nistz256_mul_by_3");
116 &mov ("esi",&wparam(1));
117 # multiplication by 3 is performed
118 # as 2*n+n, but we can't use output
119 # to store 2*n, because if output
120 # pointer equals to input, then
122 &stack_push(8); # therefore we need to allocate
123 # 256-bit intermediate buffer.
126 &call ("_ecp_nistz256_add");
127 &lea ("esi",&DWP(0,"edi"));
128 &mov ("ebp",&wparam(1));
129 &mov ("edi",&wparam(0));
130 &call ("_ecp_nistz256_add");
132 &function_end("ecp_nistz256_mul_by_3");
134 ########################################################################
135 # void ecp_nistz256_div_by_2(BN_ULONG edi[8],const BN_ULONG esi[8]);
136 &function_begin("ecp_nistz256_div_by_2");
137 &mov ("esi",&wparam(1));
138 &mov ("edi",&wparam(0));
139 &call ("_ecp_nistz256_div_by_2");
140 &function_end("ecp_nistz256_div_by_2");
142 &function_begin_B("_ecp_nistz256_div_by_2");
143 # tmp = a is odd ? a+mod : a
145 # note that because mod has special form, i.e. consists of
146 # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
147 # assigning least significant bit of input to one register,
148 # %ebp, and its negative to another, %edx.
150 &mov ("ebp",&DWP(0,"esi"));
152 &mov ("ebx",&DWP(4,"esi"));
155 &mov ("ecx",&DWP(8,"esi"));
160 &mov (&DWP(0,"edi"),"eax");
162 &mov (&DWP(4,"edi"),"ebx");
163 &mov (&DWP(8,"edi"),"ecx");
165 &mov ("eax",&DWP(12,"esi"));
166 &mov ("ebx",&DWP(16,"esi"));
168 &mov ("ecx",&DWP(20,"esi"));
170 &mov (&DWP(12,"edi"),"eax");
172 &mov (&DWP(16,"edi"),"ebx");
173 &mov (&DWP(20,"edi"),"ecx");
175 &mov ("eax",&DWP(24,"esi"));
176 &mov ("ebx",&DWP(28,"esi"));
179 &mov (&DWP(24,"edi"),"eax");
180 &sbb ("esi","esi"); # broadcast carry bit
181 &mov (&DWP(28,"edi"),"ebx");
185 &mov ("eax",&DWP(0,"edi"));
186 &mov ("ebx",&DWP(4,"edi"));
187 &mov ("ecx",&DWP(8,"edi"));
188 &mov ("edx",&DWP(12,"edi"));
198 &mov (&DWP(0,"edi"),"eax");
200 &mov ("eax",&DWP(16,"edi"));
205 &mov (&DWP(4,"edi"),"ebp");
207 &mov ("ebp",&DWP(20,"edi"));
212 &mov (&DWP(8,"edi"),"ebx");
214 &mov ("ebx",&DWP(24,"edi"));
219 &mov (&DWP(12,"edi"),"ecx");
221 &mov ("ecx",&DWP(28,"edi"));
226 &mov (&DWP(16,"edi"),"edx");
232 &mov (&DWP(20,"edi"),"eax");
237 &mov (&DWP(24,"edi"),"ebp");
238 &or ("ebx","esi"); # handle top-most carry bit
239 &mov (&DWP(28,"edi"),"ebx");
242 &function_end_B("_ecp_nistz256_div_by_2");
244 ########################################################################
245 # void ecp_nistz256_add(BN_ULONG edi[8],const BN_ULONG esi[8],
246 # const BN_ULONG ebp[8]);
247 &function_begin("ecp_nistz256_add");
248 &mov ("esi",&wparam(1));
249 &mov ("ebp",&wparam(2));
250 &mov ("edi",&wparam(0));
251 &call ("_ecp_nistz256_add");
252 &function_end("ecp_nistz256_add");
254 &function_begin_B("_ecp_nistz256_add");
255 &mov ("eax",&DWP(0,"esi"));
256 &mov ("ebx",&DWP(4,"esi"));
257 &mov ("ecx",&DWP(8,"esi"));
258 &add ("eax",&DWP(0,"ebp"));
259 &mov ("edx",&DWP(12,"esi"));
260 &adc ("ebx",&DWP(4,"ebp"));
261 &mov (&DWP(0,"edi"),"eax");
262 &adc ("ecx",&DWP(8,"ebp"));
263 &mov (&DWP(4,"edi"),"ebx");
264 &adc ("edx",&DWP(12,"ebp"));
265 &mov (&DWP(8,"edi"),"ecx");
266 &mov (&DWP(12,"edi"),"edx");
268 &mov ("eax",&DWP(16,"esi"));
269 &mov ("ebx",&DWP(20,"esi"));
270 &mov ("ecx",&DWP(24,"esi"));
271 &adc ("eax",&DWP(16,"ebp"));
272 &mov ("edx",&DWP(28,"esi"));
273 &adc ("ebx",&DWP(20,"ebp"));
274 &mov (&DWP(16,"edi"),"eax");
275 &adc ("ecx",&DWP(24,"ebp"));
276 &mov (&DWP(20,"edi"),"ebx");
277 &adc ("edx",&DWP(28,"ebp"));
278 &mov (&DWP(24,"edi"),"ecx");
279 &sbb ("esi","esi"); # broadcast carry bit
280 &mov (&DWP(28,"edi"),"edx");
282 # if a+b carries, subtract modulus.
284 # Note that because mod has special form, i.e. consists of
285 # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
286 # assigning carry bit to one register, %ebp, and its negative
287 # to another, %esi. But we started by calculating %esi...
289 &mov ("eax",&DWP(0,"edi"));
291 &mov ("ebx",&DWP(4,"edi"));
293 &mov ("ecx",&DWP(8,"edi"));
295 &mov ("edx",&DWP(12,"edi"));
297 &mov (&DWP(0,"edi"),"eax");
299 &mov (&DWP(4,"edi"),"ebx");
301 &mov (&DWP(8,"edi"),"ecx");
302 &mov (&DWP(12,"edi"),"edx");
304 &mov ("eax",&DWP(16,"edi"));
305 &mov ("ebx",&DWP(20,"edi"));
306 &mov ("ecx",&DWP(24,"edi"));
308 &mov ("edx",&DWP(28,"edi"));
310 &mov (&DWP(16,"edi"),"eax");
312 &mov (&DWP(20,"edi"),"ebx");
314 &mov (&DWP(24,"edi"),"ecx");
315 &mov (&DWP(28,"edi"),"edx");
318 &function_end_B("_ecp_nistz256_add");
320 ########################################################################
321 # void ecp_nistz256_sub(BN_ULONG edi[8],const BN_ULONG esi[8],
322 # const BN_ULONG ebp[8]);
323 &function_begin("ecp_nistz256_sub");
324 &mov ("esi",&wparam(1));
325 &mov ("ebp",&wparam(2));
326 &mov ("edi",&wparam(0));
327 &call ("_ecp_nistz256_sub");
328 &function_end("ecp_nistz256_sub");
330 &function_begin_B("_ecp_nistz256_sub");
331 &mov ("eax",&DWP(0,"esi"));
332 &mov ("ebx",&DWP(4,"esi"));
333 &mov ("ecx",&DWP(8,"esi"));
334 &sub ("eax",&DWP(0,"ebp"));
335 &mov ("edx",&DWP(12,"esi"));
336 &sbb ("ebx",&DWP(4,"ebp"));
337 &mov (&DWP(0,"edi"),"eax");
338 &sbb ("ecx",&DWP(8,"ebp"));
339 &mov (&DWP(4,"edi"),"ebx");
340 &sbb ("edx",&DWP(12,"ebp"));
341 &mov (&DWP(8,"edi"),"ecx");
342 &mov (&DWP(12,"edi"),"edx");
344 &mov ("eax",&DWP(16,"esi"));
345 &mov ("ebx",&DWP(20,"esi"));
346 &mov ("ecx",&DWP(24,"esi"));
347 &sbb ("eax",&DWP(16,"ebp"));
348 &mov ("edx",&DWP(28,"esi"));
349 &sbb ("ebx",&DWP(20,"ebp"));
350 &sbb ("ecx",&DWP(24,"ebp"));
351 &mov (&DWP(16,"edi"),"eax");
352 &sbb ("edx",&DWP(28,"ebp"));
353 &mov (&DWP(20,"edi"),"ebx");
354 &sbb ("esi","esi"); # broadcast borrow bit
355 &mov (&DWP(24,"edi"),"ecx");
356 &mov (&DWP(28,"edi"),"edx");
358 # if a-b borrows, add modulus.
360 # Note that because mod has special form, i.e. consists of
361 # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
362 # assigning borrow bit to one register, %ebp, and its negative
363 # to another, %esi. But we started by calculating %esi...
365 &mov ("eax",&DWP(0,"edi"));
367 &mov ("ebx",&DWP(4,"edi"));
369 &mov ("ecx",&DWP(8,"edi"));
371 &mov ("edx",&DWP(12,"edi"));
373 &mov (&DWP(0,"edi"),"eax");
375 &mov (&DWP(4,"edi"),"ebx");
377 &mov (&DWP(8,"edi"),"ecx");
378 &mov (&DWP(12,"edi"),"edx");
380 &mov ("eax",&DWP(16,"edi"));
381 &mov ("ebx",&DWP(20,"edi"));
382 &mov ("ecx",&DWP(24,"edi"));
384 &mov ("edx",&DWP(28,"edi"));
386 &mov (&DWP(16,"edi"),"eax");
388 &mov (&DWP(20,"edi"),"ebx");
390 &mov (&DWP(24,"edi"),"ecx");
391 &mov (&DWP(28,"edi"),"edx");
394 &function_end_B("_ecp_nistz256_sub");
396 ########################################################################
397 # void ecp_nistz256_neg(BN_ULONG edi[8],const BN_ULONG esi[8]);
398 &function_begin("ecp_nistz256_neg");
399 &mov ("ebp",&wparam(1));
400 &mov ("edi",&wparam(0));
404 &mov (&DWP(0,"esp"),"eax");
406 &mov (&DWP(4,"esp"),"eax");
407 &mov (&DWP(8,"esp"),"eax");
408 &mov (&DWP(12,"esp"),"eax");
409 &mov (&DWP(16,"esp"),"eax");
410 &mov (&DWP(20,"esp"),"eax");
411 &mov (&DWP(24,"esp"),"eax");
412 &mov (&DWP(28,"esp"),"eax");
414 &call ("_ecp_nistz256_sub");
417 &function_end("ecp_nistz256_neg");
419 &function_begin_B("_picup_eax");
420 &mov ("eax",&DWP(0,"esp"));
422 &function_end_B("_picup_eax");
424 ########################################################################
425 # void ecp_nistz256_to_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
426 &function_begin("ecp_nistz256_to_mont");
427 &mov ("esi",&wparam(1));
428 &call ("_picup_eax");
430 &lea ("ebp",&DWP(&label("RR")."-".&label("pic"),"eax"));
432 &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
433 &mov ("eax",&DWP(0,"eax")); }
434 &mov ("edi",&wparam(0));
435 &call ("_ecp_nistz256_mul_mont");
436 &function_end("ecp_nistz256_to_mont");
438 ########################################################################
439 # void ecp_nistz256_from_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
440 &function_begin("ecp_nistz256_from_mont");
441 &mov ("esi",&wparam(1));
442 &call ("_picup_eax");
444 &lea ("ebp",&DWP(&label("ONE")."-".&label("pic"),"eax"));
446 &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
447 &mov ("eax",&DWP(0,"eax")); }
448 &mov ("edi",&wparam(0));
449 &call ("_ecp_nistz256_mul_mont");
450 &function_end("ecp_nistz256_from_mont");
452 ########################################################################
453 # void ecp_nistz256_mul_mont(BN_ULONG edi[8],const BN_ULONG esi[8],
454 # const BN_ULONG ebp[8]);
455 &function_begin("ecp_nistz256_mul_mont");
456 &mov ("esi",&wparam(1));
457 &mov ("ebp",&wparam(2));
459 &call ("_picup_eax");
461 &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
462 &mov ("eax",&DWP(0,"eax")); }
463 &mov ("edi",&wparam(0));
464 &call ("_ecp_nistz256_mul_mont");
465 &function_end("ecp_nistz256_mul_mont");
467 ########################################################################
468 # void ecp_nistz256_sqr_mont(BN_ULONG edi[8],const BN_ULONG esi[8]);
469 &function_begin("ecp_nistz256_sqr_mont");
470 &mov ("esi",&wparam(1));
472 &call ("_picup_eax");
474 &picmeup("eax","OPENSSL_ia32cap_P","eax",&label("pic"));
475 &mov ("eax",&DWP(0,"eax")); }
476 &mov ("edi",&wparam(0));
478 &call ("_ecp_nistz256_mul_mont");
479 &function_end("ecp_nistz256_sqr_mont");
481 &function_begin_B("_ecp_nistz256_mul_mont");
483 &and ("eax",1<<24|1<<26);
484 &cmp ("eax",1<<24|1<<26); # see if XMM+SSE2 is on
485 &jne (&label("mul_mont_ialu"));
487 ########################################
488 # SSE2 code path featuring 32x16-bit
489 # multiplications is ~2x faster than
490 # IALU counterpart (except on Atom)...
491 ########################################
493 # +------------------------------------+< %esp
494 # | 7 16-byte temporary XMM words, |
495 # | "sliding" toward lower address |
497 # +------------------------------------+
498 # | unused XMM word |
499 # +------------------------------------+< +128,%ebx
500 # | 8 16-byte XMM words holding copies |
501 # | of a[i]<<64|a[i] |
504 # +------------------------------------+< +256
508 &movd ("xmm7",&DWP(0,"ebp")); # b[0] -> 0000.00xy
509 &lea ("ebp",&DWP(4,"ebp"));
510 &pcmpeqd("xmm6","xmm6");
511 &psrlq ("xmm6",48); # compose 0xffff<<64|0xffff
513 &pshuflw("xmm7","xmm7",0b11011100); # 0000.00xy -> 0000.0x0y
515 &pshufd ("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
516 &lea ("ebx",&DWP(0x80,"esp"));
518 &movd ("xmm0",&DWP(4*0,"esi")); # a[0] -> 0000.00xy
519 &pshufd ("xmm0","xmm0",0b11001100); # 0000.00xy -> 00xy.00xy
520 &movd ("xmm1",&DWP(4*1,"esi")); # a[1] -> ...
521 &movdqa (&QWP(0x00,"ebx"),"xmm0"); # offload converted a[0]
522 &pmuludq("xmm0","xmm7"); # a[0]*b[0]
524 &movd ("xmm2",&DWP(4*2,"esi"));
525 &pshufd ("xmm1","xmm1",0b11001100);
526 &movdqa (&QWP(0x10,"ebx"),"xmm1");
527 &pmuludq("xmm1","xmm7"); # a[1]*b[0]
529 &movq ("xmm4","xmm0"); # clear upper 64 bits
531 &paddq ("xmm4","xmm0");
532 &movdqa("xmm5","xmm4");
533 &psrldq("xmm4",10); # upper 32 bits of a[0]*b[0]
534 &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[0]
536 # Upper half of a[0]*b[i] is carried into next multiplication
537 # iteration, while lower one "participates" in actual reduction.
538 # Normally latter is done by accumulating result of multiplication
539 # of modulus by "magic" digit, but thanks to special form of modulus
540 # and "magic" digit it can be performed only with additions and
541 # subtractions (see note in IALU section below). Note that we are
542 # not bothered with carry bits, they are accumulated in "flatten"
543 # phase after all multiplications and reductions.
545 &movd ("xmm3",&DWP(4*3,"esi"));
546 &pshufd ("xmm2","xmm2",0b11001100);
547 &movdqa (&QWP(0x20,"ebx"),"xmm2");
548 &pmuludq("xmm2","xmm7"); # a[2]*b[0]
549 &paddq ("xmm1","xmm4"); # a[1]*b[0]+hw(a[0]*b[0]), carry
550 &movdqa (&QWP(0x00,"esp"),"xmm1"); # t[0]
552 &movd ("xmm0",&DWP(4*4,"esi"));
553 &pshufd ("xmm3","xmm3",0b11001100);
554 &movdqa (&QWP(0x30,"ebx"),"xmm3");
555 &pmuludq("xmm3","xmm7"); # a[3]*b[0]
556 &movdqa (&QWP(0x10,"esp"),"xmm2");
558 &movd ("xmm1",&DWP(4*5,"esi"));
559 &pshufd ("xmm0","xmm0",0b11001100);
560 &movdqa (&QWP(0x40,"ebx"),"xmm0");
561 &pmuludq("xmm0","xmm7"); # a[4]*b[0]
562 &paddq ("xmm3","xmm5"); # a[3]*b[0]+lw(a[0]*b[0]), reduction step
563 &movdqa (&QWP(0x20,"esp"),"xmm3");
565 &movd ("xmm2",&DWP(4*6,"esi"));
566 &pshufd ("xmm1","xmm1",0b11001100);
567 &movdqa (&QWP(0x50,"ebx"),"xmm1");
568 &pmuludq("xmm1","xmm7"); # a[5]*b[0]
569 &movdqa (&QWP(0x30,"esp"),"xmm0");
570 &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
572 &movd ("xmm3",&DWP(4*7,"esi"));
573 &pshufd ("xmm2","xmm2",0b11001100);
574 &movdqa (&QWP(0x60,"ebx"),"xmm2");
575 &pmuludq("xmm2","xmm7"); # a[6]*b[0]
576 &movdqa (&QWP(0x40,"esp"),"xmm1");
577 &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
579 &movd ("xmm0",&DWP(0,"ebp")); # b[1] -> 0000.00xy
580 &pshufd ("xmm3","xmm3",0b11001100);
581 &movdqa (&QWP(0x70,"ebx"),"xmm3");
582 &pmuludq("xmm3","xmm7"); # a[7]*b[0]
584 &pshuflw("xmm7","xmm0",0b11011100); # 0000.00xy -> 0000.0x0y
585 &movdqa ("xmm0",&QWP(0x00,"ebx")); # pre-load converted a[0]
586 &pshufd ("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
589 &lea ("ebp",&DWP(4,"ebp"));
590 &jmp (&label("madd_sse2"));
592 &set_label("madd_sse2",16);
593 &paddq ("xmm2","xmm5"); # a[6]*b[i-1]+lw(a[0]*b[i-1]), reduction step [modulo-scheduled]
594 &paddq ("xmm3","xmm4"); # a[7]*b[i-1]+lw(a[0]*b[i-1])*0xffffffff, reduction step [modulo-scheduled]
595 &movdqa ("xmm1",&QWP(0x10,"ebx"));
596 &pmuludq("xmm0","xmm7"); # a[0]*b[i]
597 &movdqa(&QWP(0x50,"esp"),"xmm2");
599 &movdqa ("xmm2",&QWP(0x20,"ebx"));
600 &pmuludq("xmm1","xmm7"); # a[1]*b[i]
601 &movdqa(&QWP(0x60,"esp"),"xmm3");
602 &paddq ("xmm0",&QWP(0x00,"esp"));
604 &movdqa ("xmm3",&QWP(0x30,"ebx"));
605 &pmuludq("xmm2","xmm7"); # a[2]*b[i]
606 &movq ("xmm4","xmm0"); # clear upper 64 bits
608 &paddq ("xmm1",&QWP(0x10,"esp"));
609 &paddq ("xmm4","xmm0");
610 &movdqa("xmm5","xmm4");
611 &psrldq("xmm4",10); # upper 33 bits of a[0]*b[i]+t[0]
613 &movdqa ("xmm0",&QWP(0x40,"ebx"));
614 &pmuludq("xmm3","xmm7"); # a[3]*b[i]
615 &paddq ("xmm1","xmm4"); # a[1]*b[i]+hw(a[0]*b[i]), carry
616 &paddq ("xmm2",&QWP(0x20,"esp"));
617 &movdqa (&QWP(0x00,"esp"),"xmm1");
619 &movdqa ("xmm1",&QWP(0x50,"ebx"));
620 &pmuludq("xmm0","xmm7"); # a[4]*b[i]
621 &paddq ("xmm3",&QWP(0x30,"esp"));
622 &movdqa (&QWP(0x10,"esp"),"xmm2");
623 &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[i]
625 &movdqa ("xmm2",&QWP(0x60,"ebx"));
626 &pmuludq("xmm1","xmm7"); # a[5]*b[i]
627 &paddq ("xmm3","xmm5"); # a[3]*b[i]+lw(a[0]*b[i]), reduction step
628 &paddq ("xmm0",&QWP(0x40,"esp"));
629 &movdqa (&QWP(0x20,"esp"),"xmm3");
630 &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
632 &movdqa ("xmm3","xmm7");
633 &pmuludq("xmm2","xmm7"); # a[6]*b[i]
634 &movd ("xmm7",&DWP(0,"ebp")); # b[i++] -> 0000.00xy
635 &lea ("ebp",&DWP(4,"ebp"));
636 &paddq ("xmm1",&QWP(0x50,"esp"));
637 &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
638 &movdqa (&QWP(0x30,"esp"),"xmm0");
639 &pshuflw("xmm7","xmm7",0b11011100); # 0000.00xy -> 0000.0x0y
641 &pmuludq("xmm3",&QWP(0x70,"ebx")); # a[7]*b[i]
642 &pshufd("xmm7","xmm7",0b11011100); # 0000.0x0y -> 000x.000y
643 &movdqa("xmm0",&QWP(0x00,"ebx")); # pre-load converted a[0]
644 &movdqa (&QWP(0x40,"esp"),"xmm1");
645 &paddq ("xmm2",&QWP(0x60,"esp"));
648 &jnz (&label("madd_sse2"));
650 &paddq ("xmm2","xmm5"); # a[6]*b[6]+lw(a[0]*b[6]), reduction step [modulo-scheduled]
651 &paddq ("xmm3","xmm4"); # a[7]*b[6]+lw(a[0]*b[6])*0xffffffff, reduction step [modulo-scheduled]
652 &movdqa ("xmm1",&QWP(0x10,"ebx"));
653 &pmuludq("xmm0","xmm7"); # a[0]*b[7]
654 &movdqa(&QWP(0x50,"esp"),"xmm2");
656 &movdqa ("xmm2",&QWP(0x20,"ebx"));
657 &pmuludq("xmm1","xmm7"); # a[1]*b[7]
658 &movdqa(&QWP(0x60,"esp"),"xmm3");
659 &paddq ("xmm0",&QWP(0x00,"esp"));
661 &movdqa ("xmm3",&QWP(0x30,"ebx"));
662 &pmuludq("xmm2","xmm7"); # a[2]*b[7]
663 &movq ("xmm4","xmm0"); # clear upper 64 bits
665 &paddq ("xmm1",&QWP(0x10,"esp"));
666 &paddq ("xmm4","xmm0");
667 &movdqa("xmm5","xmm4");
668 &psrldq("xmm4",10); # upper 33 bits of a[0]*b[i]+t[0]
670 &movdqa ("xmm0",&QWP(0x40,"ebx"));
671 &pmuludq("xmm3","xmm7"); # a[3]*b[7]
672 &paddq ("xmm1","xmm4"); # a[1]*b[7]+hw(a[0]*b[7]), carry
673 &paddq ("xmm2",&QWP(0x20,"esp"));
674 &movdqa (&QWP(0x00,"esp"),"xmm1");
676 &movdqa ("xmm1",&QWP(0x50,"ebx"));
677 &pmuludq("xmm0","xmm7"); # a[4]*b[7]
678 &paddq ("xmm3",&QWP(0x30,"esp"));
679 &movdqa (&QWP(0x10,"esp"),"xmm2");
680 &pand ("xmm5","xmm6"); # lower 32 bits of a[0]*b[i]
682 &movdqa ("xmm2",&QWP(0x60,"ebx"));
683 &pmuludq("xmm1","xmm7"); # a[5]*b[7]
684 &paddq ("xmm3","xmm5"); # reduction step
685 &paddq ("xmm0",&QWP(0x40,"esp"));
686 &movdqa (&QWP(0x20,"esp"),"xmm3");
687 &pshufd("xmm4","xmm5",0b10110001); # xmm4 = xmm5<<32, reduction step
689 &movdqa ("xmm3",&QWP(0x70,"ebx"));
690 &pmuludq("xmm2","xmm7"); # a[6]*b[7]
691 &paddq ("xmm1",&QWP(0x50,"esp"));
692 &psubq ("xmm4","xmm5"); # xmm4 = xmm5*0xffffffff, reduction step
693 &movdqa (&QWP(0x30,"esp"),"xmm0");
695 &pmuludq("xmm3","xmm7"); # a[7]*b[7]
696 &pcmpeqd("xmm7","xmm7");
697 &movdqa ("xmm0",&QWP(0x00,"esp"));
699 &movdqa (&QWP(0x40,"esp"),"xmm1");
700 &paddq ("xmm2",&QWP(0x60,"esp"));
702 &paddq ("xmm2","xmm5"); # a[6]*b[7]+lw(a[0]*b[7]), reduction step
703 &paddq ("xmm3","xmm4"); # a[6]*b[7]+lw(a[0]*b[7])*0xffffffff, reduction step
704 &movdqa(&QWP(0x50,"esp"),"xmm2");
705 &movdqa(&QWP(0x60,"esp"),"xmm3");
707 &movdqa ("xmm1",&QWP(0x10,"esp"));
708 &movdqa ("xmm2",&QWP(0x20,"esp"));
709 &movdqa ("xmm3",&QWP(0x30,"esp"));
711 &movq ("xmm4","xmm0"); # "flatten"
712 &pand ("xmm0","xmm7");
715 &movq ("xmm5","xmm1");
716 &paddq ("xmm0","xmm4");
717 &pand ("xmm1","xmm7");
719 &movd ("eax","xmm0");
722 &paddq ("xmm5","xmm0");
723 &movdqa ("xmm0",&QWP(0x40,"esp"));
724 &sub ("eax",-1); # start subtracting modulus,
725 # this is used to determine
726 # if result is larger/smaller
727 # than modulus (see below)
729 &movq ("xmm4","xmm2");
730 &paddq ("xmm1","xmm5");
731 &pand ("xmm2","xmm7");
733 &mov (&DWP(4*0,"edi"),"eax");
734 &movd ("eax","xmm1");
737 &paddq ("xmm4","xmm1");
738 &movdqa ("xmm1",&QWP(0x50,"esp"));
741 &movq ("xmm5","xmm3");
742 &paddq ("xmm2","xmm4");
743 &pand ("xmm3","xmm7");
745 &mov (&DWP(4*1,"edi"),"eax");
746 &movd ("eax","xmm2");
749 &paddq ("xmm5","xmm2");
750 &movdqa ("xmm2",&QWP(0x60,"esp"));
753 &movq ("xmm4","xmm0");
754 &paddq ("xmm3","xmm5");
755 &pand ("xmm0","xmm7");
757 &mov (&DWP(4*2,"edi"),"eax");
758 &movd ("eax","xmm3");
761 &paddq ("xmm4","xmm3");
764 &movq ("xmm5","xmm1");
765 &paddq ("xmm0","xmm4");
766 &pand ("xmm1","xmm7");
768 &mov (&DWP(4*3,"edi"),"eax");
769 &movd ("eax","xmm0");
772 &paddq ("xmm5","xmm0");
775 &movq ("xmm4","xmm2");
776 &paddq ("xmm1","xmm5");
777 &pand ("xmm2","xmm7");
779 &movd ("ebx","xmm1");
783 &paddq ("xmm4","xmm1");
785 &paddq ("xmm2","xmm4");
787 &movd ("ecx","xmm2");
790 &movd ("edx","xmm2");
791 &pextrw ("esi","xmm2",2); # top-most overflow bit
794 &sbb ("esi",0); # borrow from subtraction
796 # Final step is "if result > mod, subtract mod", and at this point
797 # we have result - mod written to output buffer, as well as borrow
798 # bit from this subtraction, and if borrow bit is set, we add
801 # Note that because mod has special form, i.e. consists of
802 # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
803 # assigning borrow bit to one register, %ebp, and its negative
804 # to another, %esi. But we started by calculating %esi...
807 &add (&DWP(4*0,"edi"),"esi"); # add modulus or zero
808 &adc (&DWP(4*1,"edi"),"esi");
809 &adc (&DWP(4*2,"edi"),"esi");
810 &adc (&DWP(4*3,"edi"),0);
813 &mov (&DWP(4*4,"edi"),"eax");
815 &mov (&DWP(4*5,"edi"),"ebx");
817 &mov (&DWP(4*6,"edi"),"ecx");
818 &mov (&DWP(4*7,"edi"),"edx");
822 &set_label("mul_mont_ialu",16); }
824 ########################################
825 # IALU code path suitable for all CPUs.
826 ########################################
828 # +------------------------------------+< %esp
829 # | 8 32-bit temporary words, accessed |
830 # | as circular buffer |
833 # +------------------------------------+< +32
834 # | offloaded destination pointer |
835 # +------------------------------------+
837 # +------------------------------------+< +40
840 &mov ("eax",&DWP(0*4,"esi")); # a[0]
841 &mov ("ebx",&DWP(0*4,"ebp")); # b[0]
842 &mov (&DWP(8*4,"esp"),"edi"); # off-load dst ptr
844 &mul ("ebx"); # a[0]*b[0]
845 &mov (&DWP(0*4,"esp"),"eax"); # t[0]
846 &mov ("eax",&DWP(1*4,"esi"));
849 &mul ("ebx"); # a[1]*b[0]
851 &mov ("eax",&DWP(2*4,"esi"));
853 &mov (&DWP(1*4,"esp"),"ecx"); # t[1]
856 &mul ("ebx"); # a[2]*b[0]
858 &mov ("eax",&DWP(3*4,"esi"));
860 &mov (&DWP(2*4,"esp"),"ecx"); # t[2]
863 &mul ("ebx"); # a[3]*b[0]
865 &mov ("eax",&DWP(4*4,"esi"));
867 &mov (&DWP(3*4,"esp"),"ecx"); # t[3]
870 &mul ("ebx"); # a[4]*b[0]
872 &mov ("eax",&DWP(5*4,"esi"));
874 &mov (&DWP(4*4,"esp"),"ecx"); # t[4]
877 &mul ("ebx"); # a[5]*b[0]
879 &mov ("eax",&DWP(6*4,"esi"));
881 &mov (&DWP(5*4,"esp"),"ecx"); # t[5]
884 &mul ("ebx"); # a[6]*b[0]
886 &mov ("eax",&DWP(7*4,"esi"));
888 &mov (&DWP(6*4,"esp"),"ecx"); # t[6]
891 &xor ("edi","edi"); # initial top-most carry
892 &mul ("ebx"); # a[7]*b[0]
893 &add ("ecx","eax"); # t[7]
894 &mov ("eax",&DWP(0*4,"esp")); # t[0]
895 &adc ("edx",0); # t[8]
897 for ($i=0;$i<7;$i++) {
900 # Reduction iteration is normally performed by accumulating
901 # result of multiplication of modulus by "magic" digit [and
902 # omitting least significant word, which is guaranteed to
903 # be 0], but thanks to special form of modulus and "magic"
904 # digit being equal to least significant word, it can be
905 # performed with additions and subtractions alone. Indeed:
907 # ffff.0001.0000.0000.0000.ffff.ffff.ffff
909 # + xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
911 # Now observing that ff..ff*x = (2^n-1)*x = 2^n*x-x, we
914 # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.abcd
915 # + abcd.0000.abcd.0000.0000.abcd.0000.0000.0000
916 # - abcd.0000.0000.0000.0000.0000.0000.abcd
918 # or marking redundant operations:
920 # xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.----
921 # + abcd.0000.abcd.0000.0000.abcd.----.----.----
922 # - abcd.----.----.----.----.----.----.----
924 &add (&DWP((($i+3)%8)*4,"esp"),"eax"); # t[3]+=t[0]
925 &adc (&DWP((($i+4)%8)*4,"esp"),0); # t[4]+=0
926 &adc (&DWP((($i+5)%8)*4,"esp"),0); # t[5]+=0
927 &adc (&DWP((($i+6)%8)*4,"esp"),"eax"); # t[6]+=t[0]
928 &adc ("ecx",0); # t[7]+=0
929 &adc ("edx","eax"); # t[8]+=t[0]
930 &adc ("edi",0); # top-most carry
931 &mov ("ebx",&DWP($j*4,"ebp")); # b[i]
932 &sub ("ecx","eax"); # t[7]-=t[0]
933 &mov ("eax",&DWP(0*4,"esi")); # a[0]
934 &sbb ("edx",0); # t[8]-=0
935 &mov (&DWP((($i+7)%8)*4,"esp"),"ecx");
936 &sbb ("edi",0); # top-most carry,
939 # *addition* of value
940 # with (abcd<<32)-abcd
943 # impossible, because
946 &mov (&DWP((($i+8)%8)*4,"esp"),"edx");
948 &mul ("ebx"); # a[0]*b[i]
949 &add ("eax",&DWP((($j+0)%8)*4,"esp"));
951 &mov (&DWP((($j+0)%8)*4,"esp"),"eax");
952 &mov ("eax",&DWP(1*4,"esi"));
955 &mul ("ebx"); # a[1]*b[i]
956 &add ("ecx",&DWP((($j+1)%8)*4,"esp"));
960 &mov ("eax",&DWP(2*4,"esi"));
961 &mov (&DWP((($j+1)%8)*4,"esp"),"ecx");
964 &mul ("ebx"); # a[2]*b[i]
965 &add ("ecx",&DWP((($j+2)%8)*4,"esp"));
969 &mov ("eax",&DWP(3*4,"esi"));
970 &mov (&DWP((($j+2)%8)*4,"esp"),"ecx");
973 &mul ("ebx"); # a[3]*b[i]
974 &add ("ecx",&DWP((($j+3)%8)*4,"esp"));
978 &mov ("eax",&DWP(4*4,"esi"));
979 &mov (&DWP((($j+3)%8)*4,"esp"),"ecx");
982 &mul ("ebx"); # a[4]*b[i]
983 &add ("ecx",&DWP((($j+4)%8)*4,"esp"));
987 &mov ("eax",&DWP(5*4,"esi"));
988 &mov (&DWP((($j+4)%8)*4,"esp"),"ecx");
991 &mul ("ebx"); # a[5]*b[i]
992 &add ("ecx",&DWP((($j+5)%8)*4,"esp"));
996 &mov ("eax",&DWP(6*4,"esi"));
997 &mov (&DWP((($j+5)%8)*4,"esp"),"ecx");
1000 &mul ("ebx"); # a[6]*b[i]
1001 &add ("ecx",&DWP((($j+6)%8)*4,"esp"));
1005 &mov ("eax",&DWP(7*4,"esi"));
1006 &mov (&DWP((($j+6)%8)*4,"esp"),"ecx");
1009 &mul ("ebx"); # a[7]*b[i]
1010 &add ("ecx",&DWP((($j+7)%8)*4,"esp"));
1012 &add ("ecx","eax"); # t[7]
1013 &mov ("eax",&DWP((($j+0)%8)*4,"esp")); # t[0]
1014 &adc ("edx","edi"); # t[8]
1016 &adc ("edi",0); # top-most carry
1018 &mov ("ebp",&DWP(8*4,"esp")); # restore dst ptr
1022 # last multiplication-less reduction
1023 &add (&DWP((($i+3)%8)*4,"esp"),"eax"); # t[3]+=t[0]
1024 &adc (&DWP((($i+4)%8)*4,"esp"),0); # t[4]+=0
1025 &adc (&DWP((($i+5)%8)*4,"esp"),0); # t[5]+=0
1026 &adc (&DWP((($i+6)%8)*4,"esp"),"eax"); # t[6]+=t[0]
1027 &adc ("ecx",0); # t[7]+=0
1028 &adc ("edx","eax"); # t[8]+=t[0]
1029 &adc ("edi",0); # top-most carry
1030 &mov ("ebx",&DWP((($j+1)%8)*4,"esp"));
1031 &sub ("ecx","eax"); # t[7]-=t[0]
1032 &mov ("eax",&DWP((($j+0)%8)*4,"esp"));
1033 &sbb ("edx",0); # t[8]-=0
1034 &mov (&DWP((($i+7)%8)*4,"esp"),"ecx");
1035 &sbb ("edi",0); # top-most carry
1036 &mov (&DWP((($i+8)%8)*4,"esp"),"edx");
1038 # Final step is "if result > mod, subtract mod", but we do it
1039 # "other way around", namely write result - mod to output buffer
1040 # and if subtraction borrowed, add modulus back.
1042 &mov ("ecx",&DWP((($j+2)%8)*4,"esp"));
1044 &mov ("edx",&DWP((($j+3)%8)*4,"esp"));
1046 &mov (&DWP(0*4,"ebp"),"eax");
1048 &mov (&DWP(1*4,"ebp"),"ebx");
1050 &mov (&DWP(2*4,"ebp"),"ecx");
1051 &mov (&DWP(3*4,"ebp"),"edx");
1053 &mov ("eax",&DWP((($j+4)%8)*4,"esp"));
1054 &mov ("ebx",&DWP((($j+5)%8)*4,"esp"));
1055 &mov ("ecx",&DWP((($j+6)%8)*4,"esp"));
1057 &mov ("edx",&DWP((($j+7)%8)*4,"esp"));
1063 # Note that because mod has special form, i.e. consists of
1064 # 0xffffffff, 1 and 0s, we can conditionally synthesize it by
1065 # assigning borrow bit to one register, %ebp, and its negative
1066 # to another, %esi. But we started by calculating %esi...
1069 &add (&DWP(0*4,"ebp"),"edi"); # add modulus or zero
1070 &adc (&DWP(1*4,"ebp"),"edi");
1071 &adc (&DWP(2*4,"ebp"),"edi");
1072 &adc (&DWP(3*4,"ebp"),0);
1075 &mov (&DWP(4*4,"ebp"),"eax");
1077 &mov (&DWP(5*4,"ebp"),"ebx");
1079 &mov (&DWP(6*4,"ebp"),"ecx");
1080 &mov ("edi","ebp"); # fulfill contract
1081 &mov (&DWP(7*4,"ebp"),"edx");
1085 &function_end_B("_ecp_nistz256_mul_mont");
1087 ########################################################################
1088 # void ecp_nistz256_scatter_w5(void *edi,const P256_POINT *esi,
1090 &function_begin("ecp_nistz256_scatter_w5");
1091 &mov ("edi",&wparam(0));
1092 &mov ("esi",&wparam(1));
1093 &mov ("ebp",&wparam(2));
1095 &lea ("edi",&DWP(128-4,"edi","ebp",4));
1097 &set_label("scatter_w5_loop");
1098 &mov ("eax",&DWP(0,"esi"));
1099 &mov ("ebx",&DWP(4,"esi"));
1100 &mov ("ecx",&DWP(8,"esi"));
1101 &mov ("edx",&DWP(12,"esi"));
1102 &lea ("esi",&DWP(16,"esi"));
1103 &mov (&DWP(64*0-128,"edi"),"eax");
1104 &mov (&DWP(64*1-128,"edi"),"ebx");
1105 &mov (&DWP(64*2-128,"edi"),"ecx");
1106 &mov (&DWP(64*3-128,"edi"),"edx");
1107 &lea ("edi",&DWP(64*4,"edi"));
1109 &jnz (&label("scatter_w5_loop"));
1110 &function_end("ecp_nistz256_scatter_w5");
1112 ########################################################################
1113 # void ecp_nistz256_gather_w5(P256_POINT *edi,const void *esi,
1115 &function_begin("ecp_nistz256_gather_w5");
1116 &mov ("esi",&wparam(1));
1117 &mov ("ebp",&wparam(2));
1119 &lea ("esi",&DWP(0,"esi","ebp",4));
1122 &mov ("edi",&wparam(0));
1123 &lea ("esi",&DWP(0,"esi","ebp",4));
1125 for($i=0;$i<24;$i+=4) {
1126 &mov ("eax",&DWP(64*($i+0),"esi"));
1127 &mov ("ebx",&DWP(64*($i+1),"esi"));
1128 &mov ("ecx",&DWP(64*($i+2),"esi"));
1129 &mov ("edx",&DWP(64*($i+3),"esi"));
1134 &mov (&DWP(4*($i+0),"edi"),"eax");
1135 &mov (&DWP(4*($i+1),"edi"),"ebx");
1136 &mov (&DWP(4*($i+2),"edi"),"ecx");
1137 &mov (&DWP(4*($i+3),"edi"),"edx");
1139 &function_end("ecp_nistz256_gather_w5");
1141 ########################################################################
1142 # void ecp_nistz256_scatter_w7(void *edi,const P256_POINT_AFFINE *esi,
1144 &function_begin("ecp_nistz256_scatter_w7");
1145 &mov ("edi",&wparam(0));
1146 &mov ("esi",&wparam(1));
1147 &mov ("ebp",&wparam(2));
1149 &lea ("edi",&DWP(-1,"edi","ebp"));
1151 &set_label("scatter_w7_loop");
1152 &mov ("eax",&DWP(0,"esi"));
1153 &lea ("esi",&DWP(4,"esi"));
1154 &mov (&BP(64*0,"edi"),"al");
1155 &mov (&BP(64*1,"edi"),"ah");
1157 &mov (&BP(64*2,"edi"),"al");
1158 &mov (&BP(64*3,"edi"),"ah");
1159 &lea ("edi",&DWP(64*4,"edi"));
1161 &jnz (&label("scatter_w7_loop"));
1162 &function_end("ecp_nistz256_scatter_w7");
1164 ########################################################################
1165 # void ecp_nistz256_gather_w7(P256_POINT_AFFINE *edi,const void *esi,
1167 &function_begin("ecp_nistz256_gather_w7");
1168 &mov ("esi",&wparam(1));
1169 &mov ("ebp",&wparam(2));
1174 &mov ("edi",&wparam(0));
1175 &lea ("esi",&DWP(0,"esi","ebp"));
1177 for($i=0;$i<64;$i+=4) {
1178 &movz ("eax",&BP(64*($i+0),"esi"));
1179 &movz ("ebx",&BP(64*($i+1),"esi"));
1180 &movz ("ecx",&BP(64*($i+2),"esi"));
1182 &movz ("edx",&BP(64*($i+3),"esi"));
1184 &mov (&BP($i+0,"edi"),"al");
1186 &mov (&BP($i+1,"edi"),"bl");
1188 &mov (&BP($i+2,"edi"),"cl");
1189 &mov (&BP($i+3,"edi"),"dl");
1191 &function_end("ecp_nistz256_gather_w7");
1193 ########################################################################
1194 # following subroutines are "literal" implementation of those found in
1197 ########################################################################
1198 # void ecp_nistz256_point_double(P256_POINT *out,const P256_POINT *inp);
1200 &function_begin("ecp_nistz256_point_double");
1201 { my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
1203 &mov ("esi",&wparam(1));
1205 # above map() describes stack layout with 5 temporary
1206 # 256-bit vectors on top, then we take extra word for
1207 # OPENSSL_ia32cap_P copy.
1210 &call ("_picup_eax");
1212 &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
1213 &mov ("ebp",&DWP(0,"edx")); }
1215 &mov ("eax",&DWP(0,"esi")); # copy in_x
1216 &mov ("ebx",&DWP(4,"esi"));
1217 &mov ("ecx",&DWP(8,"esi"));
1218 &mov ("edx",&DWP(12,"esi"));
1219 &mov (&DWP($in_x+0,"esp"),"eax");
1220 &mov (&DWP($in_x+4,"esp"),"ebx");
1221 &mov (&DWP($in_x+8,"esp"),"ecx");
1222 &mov (&DWP($in_x+12,"esp"),"edx");
1223 &mov ("eax",&DWP(16,"esi"));
1224 &mov ("ebx",&DWP(20,"esi"));
1225 &mov ("ecx",&DWP(24,"esi"));
1226 &mov ("edx",&DWP(28,"esi"));
1227 &mov (&DWP($in_x+16,"esp"),"eax");
1228 &mov (&DWP($in_x+20,"esp"),"ebx");
1229 &mov (&DWP($in_x+24,"esp"),"ecx");
1230 &mov (&DWP($in_x+28,"esp"),"edx");
1231 &mov (&DWP(32*5,"esp"),"ebp"); # OPENSSL_ia32cap_P copy
1233 &lea ("ebp",&DWP(32,"esi"));
1234 &lea ("esi",&DWP(32,"esi"));
1235 &lea ("edi",&DWP($S,"esp"));
1236 &call ("_ecp_nistz256_add"); # p256_mul_by_2(S, in_y);
1238 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1240 &add ("esi",&wparam(1));
1241 &lea ("edi",&DWP($Zsqr,"esp"));
1243 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Zsqr, in_z);
1245 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1246 &lea ("esi",&DWP($S,"esp"));
1247 &lea ("ebp",&DWP($S,"esp"));
1248 &lea ("edi",&DWP($S,"esp"));
1249 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(S, S);
1251 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1252 &mov ("ebp",&wparam(1));
1253 &lea ("esi",&DWP(32,"ebp"));
1254 &lea ("ebp",&DWP(64,"ebp"));
1255 &lea ("edi",&DWP($tmp0,"esp"));
1256 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(tmp0, in_z, in_y);
1258 &lea ("esi",&DWP($in_x,"esp"));
1259 &lea ("ebp",&DWP($Zsqr,"esp"));
1260 &lea ("edi",&DWP($M,"esp"));
1261 &call ("_ecp_nistz256_add"); # p256_add(M, in_x, Zsqr);
1264 &lea ("esi",&DWP($tmp0,"esp"));
1265 &lea ("ebp",&DWP($tmp0,"esp"));
1266 &add ("edi",&wparam(0));
1267 &call ("_ecp_nistz256_add"); # p256_mul_by_2(res_z, tmp0);
1269 &lea ("esi",&DWP($in_x,"esp"));
1270 &lea ("ebp",&DWP($Zsqr,"esp"));
1271 &lea ("edi",&DWP($Zsqr,"esp"));
1272 &call ("_ecp_nistz256_sub"); # p256_sub(Zsqr, in_x, Zsqr);
1274 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1275 &lea ("esi",&DWP($S,"esp"));
1276 &lea ("ebp",&DWP($S,"esp"));
1277 &lea ("edi",&DWP($tmp0,"esp"));
1278 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(tmp0, S);
1280 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1281 &lea ("esi",&DWP($M,"esp"));
1282 &lea ("ebp",&DWP($Zsqr,"esp"));
1283 &lea ("edi",&DWP($M,"esp"));
1284 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(M, M, Zsqr);
1287 &lea ("esi",&DWP($tmp0,"esp"));
1288 &add ("edi",&wparam(0));
1289 &call ("_ecp_nistz256_div_by_2"); # p256_div_by_2(res_y, tmp0);
1291 &lea ("esi",&DWP($M,"esp"));
1292 &lea ("ebp",&DWP($M,"esp"));
1293 &lea ("edi",&DWP($tmp0,"esp"));
1294 &call ("_ecp_nistz256_add"); # 1/2 p256_mul_by_3(M, M);
1296 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1297 &lea ("esi",&DWP($in_x,"esp"));
1298 &lea ("ebp",&DWP($S,"esp"));
1299 &lea ("edi",&DWP($S,"esp"));
1300 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S, S, in_x);
1302 &lea ("esi",&DWP($tmp0,"esp"));
1303 &lea ("ebp",&DWP($M,"esp"));
1304 &lea ("edi",&DWP($M,"esp"));
1305 &call ("_ecp_nistz256_add"); # 2/2 p256_mul_by_3(M, M);
1307 &lea ("esi",&DWP($S,"esp"));
1308 &lea ("ebp",&DWP($S,"esp"));
1309 &lea ("edi",&DWP($tmp0,"esp"));
1310 &call ("_ecp_nistz256_add"); # p256_mul_by_2(tmp0, S);
1312 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1313 &lea ("esi",&DWP($M,"esp"));
1314 &lea ("ebp",&DWP($M,"esp"));
1315 &mov ("edi",&wparam(0));
1316 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(res_x, M);
1318 &mov ("esi","edi"); # %edi is still res_x here
1319 &lea ("ebp",&DWP($tmp0,"esp"));
1320 &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, tmp0);
1322 &lea ("esi",&DWP($S,"esp"));
1323 &mov ("ebp","edi"); # %edi is still res_x
1324 &lea ("edi",&DWP($S,"esp"));
1325 &call ("_ecp_nistz256_sub"); # p256_sub(S, S, res_x);
1327 &mov ("eax",&DWP(32*5,"esp")); # OPENSSL_ia32cap_P copy
1328 &mov ("esi","edi"); # %edi is still &S
1329 &lea ("ebp",&DWP($M,"esp"));
1330 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S, S, M);
1333 &lea ("esi",&DWP($S,"esp"));
1334 &add ("ebp",&wparam(0));
1336 &call ("_ecp_nistz256_sub"); # p256_sub(res_y, S, res_y);
1339 } &function_end("ecp_nistz256_point_double");
1341 ########################################################################
1342 # void ecp_nistz256_point_add(P256_POINT *out,const P256_POINT *in1,
1343 # const P256_POINT *in2);
1344 &function_begin("ecp_nistz256_point_add");
1345 { my ($res_x,$res_y,$res_z,
1346 $in1_x,$in1_y,$in1_z,
1347 $in2_x,$in2_y,$in2_z,
1348 $H,$Hsqr,$R,$Rsqr,$Hcub,
1349 $U1,$U2,$S1,$S2)=map(32*$_,(0..17));
1350 my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
1352 &mov ("esi",&wparam(2));
1354 # above map() describes stack layout with 18 temporary
1355 # 256-bit vectors on top, then we take extra words for
1356 # !in1infty, !in2infty, result of check for zero and
1357 # OPENSSL_ia32cap_P copy. [one unused word for padding]
1358 &stack_push(8*18+5);
1360 &call ("_picup_eax");
1362 &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
1363 &mov ("ebp",&DWP(0,"edx")); }
1365 &lea ("edi",&DWP($in2_x,"esp"));
1366 for($i=0;$i<96;$i+=16) {
1367 &mov ("eax",&DWP($i+0,"esi")); # copy in2
1368 &mov ("ebx",&DWP($i+4,"esi"));
1369 &mov ("ecx",&DWP($i+8,"esi"));
1370 &mov ("edx",&DWP($i+12,"esi"));
1371 &mov (&DWP($i+0,"edi"),"eax");
1372 &mov (&DWP(32*18+12,"esp"),"ebp") if ($i==0);
1373 &mov ("ebp","eax") if ($i==0);
1374 &or ("ebp","eax") if ($i!=0 && $i<64);
1375 &mov (&DWP($i+4,"edi"),"ebx");
1376 &or ("ebp","ebx") if ($i<64);
1377 &mov (&DWP($i+8,"edi"),"ecx");
1378 &or ("ebp","ecx") if ($i<64);
1379 &mov (&DWP($i+12,"edi"),"edx");
1380 &or ("ebp","edx") if ($i<64);
1383 &mov ("esi",&wparam(1));
1387 &mov (&DWP(32*18+4,"esp"),"ebp"); # !in2infty
1389 &lea ("edi",&DWP($in1_x,"esp"));
1390 for($i=0;$i<96;$i+=16) {
1391 &mov ("eax",&DWP($i+0,"esi")); # copy in1
1392 &mov ("ebx",&DWP($i+4,"esi"));
1393 &mov ("ecx",&DWP($i+8,"esi"));
1394 &mov ("edx",&DWP($i+12,"esi"));
1395 &mov (&DWP($i+0,"edi"),"eax");
1396 &mov ("ebp","eax") if ($i==0);
1397 &or ("ebp","eax") if ($i!=0 && $i<64);
1398 &mov (&DWP($i+4,"edi"),"ebx");
1399 &or ("ebp","ebx") if ($i<64);
1400 &mov (&DWP($i+8,"edi"),"ecx");
1401 &or ("ebp","ecx") if ($i<64);
1402 &mov (&DWP($i+12,"edi"),"edx");
1403 &or ("ebp","edx") if ($i<64);
1409 &mov (&DWP(32*18+0,"esp"),"ebp"); # !in1infty
1411 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1412 &lea ("esi",&DWP($in2_z,"esp"));
1413 &lea ("ebp",&DWP($in2_z,"esp"));
1414 &lea ("edi",&DWP($Z2sqr,"esp"));
1415 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z2sqr, in2_z);
1417 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1418 &lea ("esi",&DWP($in1_z,"esp"));
1419 &lea ("ebp",&DWP($in1_z,"esp"));
1420 &lea ("edi",&DWP($Z1sqr,"esp"));
1421 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z1sqr, in1_z);
1423 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1424 &lea ("esi",&DWP($Z2sqr,"esp"));
1425 &lea ("ebp",&DWP($in2_z,"esp"));
1426 &lea ("edi",&DWP($S1,"esp"));
1427 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S1, Z2sqr, in2_z);
1429 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1430 &lea ("esi",&DWP($Z1sqr,"esp"));
1431 &lea ("ebp",&DWP($in1_z,"esp"));
1432 &lea ("edi",&DWP($S2,"esp"));
1433 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Z1sqr, in1_z);
1435 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1436 &lea ("esi",&DWP($in1_y,"esp"));
1437 &lea ("ebp",&DWP($S1,"esp"));
1438 &lea ("edi",&DWP($S1,"esp"));
1439 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S1, S1, in1_y);
1441 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1442 &lea ("esi",&DWP($in2_y,"esp"));
1443 &lea ("ebp",&DWP($S2,"esp"));
1444 &lea ("edi",&DWP($S2,"esp"));
1445 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S2, in2_y);
1447 &lea ("esi",&DWP($S2,"esp"));
1448 &lea ("ebp",&DWP($S1,"esp"));
1449 &lea ("edi",&DWP($R,"esp"));
1450 &call ("_ecp_nistz256_sub"); # p256_sub(R, S2, S1);
1452 &or ("ebx","eax"); # see if result is zero
1453 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1456 &or ("ebx",&DWP(0,"edi"));
1457 &or ("ebx",&DWP(4,"edi"));
1458 &lea ("esi",&DWP($in1_x,"esp"));
1459 &or ("ebx",&DWP(8,"edi"));
1460 &lea ("ebp",&DWP($Z2sqr,"esp"));
1461 &or ("ebx",&DWP(12,"edi"));
1462 &lea ("edi",&DWP($U1,"esp"));
1463 &mov (&DWP(32*18+8,"esp"),"ebx");
1465 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U1, in1_x, Z2sqr);
1467 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1468 &lea ("esi",&DWP($in2_x,"esp"));
1469 &lea ("ebp",&DWP($Z1sqr,"esp"));
1470 &lea ("edi",&DWP($U2,"esp"));
1471 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, in2_x, Z1sqr);
1473 &lea ("esi",&DWP($U2,"esp"));
1474 &lea ("ebp",&DWP($U1,"esp"));
1475 &lea ("edi",&DWP($H,"esp"));
1476 &call ("_ecp_nistz256_sub"); # p256_sub(H, U2, U1);
1478 &or ("eax","ebx"); # see if result is zero
1481 &or ("eax",&DWP(0,"edi"));
1482 &or ("eax",&DWP(4,"edi"));
1483 &or ("eax",&DWP(8,"edi"));
1484 &or ("eax",&DWP(12,"edi"));
1486 &data_byte(0x3e); # predict taken
1487 &jnz (&label("add_proceed")); # is_equal(U1,U2)?
1489 &mov ("eax",&DWP(32*18+0,"esp"));
1490 &and ("eax",&DWP(32*18+4,"esp"));
1491 &mov ("ebx",&DWP(32*18+8,"esp"));
1492 &jz (&label("add_proceed")); # (in1infty || in2infty)?
1493 &test ("ebx","ebx");
1494 &jz (&label("add_proceed")); # is_equal(S1,S2)?
1496 &mov ("edi",&wparam(0));
1499 &data_byte(0xfc,0xf3,0xab); # cld; stosd
1500 &jmp (&label("add_done"));
1502 &set_label("add_proceed",16);
1503 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1504 &lea ("esi",&DWP($R,"esp"));
1505 &lea ("ebp",&DWP($R,"esp"));
1506 &lea ("edi",&DWP($Rsqr,"esp"));
1507 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Rsqr, R);
1509 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1510 &lea ("esi",&DWP($H,"esp"));
1511 &lea ("ebp",&DWP($in1_z,"esp"));
1512 &lea ("edi",&DWP($res_z,"esp"));
1513 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, H, in1_z);
1515 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1516 &lea ("esi",&DWP($H,"esp"));
1517 &lea ("ebp",&DWP($H,"esp"));
1518 &lea ("edi",&DWP($Hsqr,"esp"));
1519 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Hsqr, H);
1521 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1522 &lea ("esi",&DWP($in2_z,"esp"));
1523 &lea ("ebp",&DWP($res_z,"esp"));
1524 &lea ("edi",&DWP($res_z,"esp"));
1525 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, res_z, in2_z);
1527 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1528 &lea ("esi",&DWP($Hsqr,"esp"));
1529 &lea ("ebp",&DWP($U1,"esp"));
1530 &lea ("edi",&DWP($U2,"esp"));
1531 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, U1, Hsqr);
1533 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1534 &lea ("esi",&DWP($H,"esp"));
1535 &lea ("ebp",&DWP($Hsqr,"esp"));
1536 &lea ("edi",&DWP($Hcub,"esp"));
1537 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(Hcub, Hsqr, H);
1539 &lea ("esi",&DWP($U2,"esp"));
1540 &lea ("ebp",&DWP($U2,"esp"));
1541 &lea ("edi",&DWP($Hsqr,"esp"));
1542 &call ("_ecp_nistz256_add"); # p256_mul_by_2(Hsqr, U2);
1544 &lea ("esi",&DWP($Rsqr,"esp"));
1545 &lea ("ebp",&DWP($Hsqr,"esp"));
1546 &lea ("edi",&DWP($res_x,"esp"));
1547 &call ("_ecp_nistz256_sub"); # p256_sub(res_x, Rsqr, Hsqr);
1549 &lea ("esi",&DWP($res_x,"esp"));
1550 &lea ("ebp",&DWP($Hcub,"esp"));
1551 &lea ("edi",&DWP($res_x,"esp"));
1552 &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, Hcub);
1554 &lea ("esi",&DWP($U2,"esp"));
1555 &lea ("ebp",&DWP($res_x,"esp"));
1556 &lea ("edi",&DWP($res_y,"esp"));
1557 &call ("_ecp_nistz256_sub"); # p256_sub(res_y, U2, res_x);
1559 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1560 &lea ("esi",&DWP($Hcub,"esp"));
1561 &lea ("ebp",&DWP($S1,"esp"));
1562 &lea ("edi",&DWP($S2,"esp"));
1563 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S1, Hcub);
1565 &mov ("eax",&DWP(32*18+12,"esp")); # OPENSSL_ia32cap_P copy
1566 &lea ("esi",&DWP($R,"esp"));
1567 &lea ("ebp",&DWP($res_y,"esp"));
1568 &lea ("edi",&DWP($res_y,"esp"));
1569 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_y, R, res_y);
1571 &lea ("esi",&DWP($res_y,"esp"));
1572 &lea ("ebp",&DWP($S2,"esp"));
1573 &lea ("edi",&DWP($res_y,"esp"));
1574 &call ("_ecp_nistz256_sub"); # p256_sub(res_y, res_y, S2);
1576 &mov ("ebp",&DWP(32*18+0,"esp")); # !in1infty
1577 &mov ("esi",&DWP(32*18+4,"esp")); # !in2infty
1578 &mov ("edi",&wparam(0));
1585 ########################################
1587 for($i=64;$i<96;$i+=4) {
1589 &and ("eax",&DWP($res_x+$i,"esp"));
1591 &and ("ebx",&DWP($in2_x+$i,"esp"));
1593 &and ("ecx",&DWP($in1_x+$i,"esp"));
1596 &mov (&DWP($i,"edi"),"eax");
1598 for($i=0;$i<64;$i+=4) {
1600 &and ("eax",&DWP($res_x+$i,"esp"));
1602 &and ("ebx",&DWP($in2_x+$i,"esp"));
1604 &and ("ecx",&DWP($in1_x+$i,"esp"));
1607 &mov (&DWP($i,"edi"),"eax");
1609 &set_label("add_done");
1611 } &function_end("ecp_nistz256_point_add");
1613 ########################################################################
1614 # void ecp_nistz256_point_add_affine(P256_POINT *out,
1615 # const P256_POINT *in1,
1616 # const P256_POINT_AFFINE *in2);
1617 &function_begin("ecp_nistz256_point_add_affine");
1619 my ($res_x,$res_y,$res_z,
1620 $in1_x,$in1_y,$in1_z,
1622 $U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr)=map(32*$_,(0..14));
1624 my @ONE_mont=(1,0,0,-1,-1,-1,-2,0);
1626 &mov ("esi",&wparam(1));
1628 # above map() describes stack layout with 15 temporary
1629 # 256-bit vectors on top, then we take extra words for
1630 # !in1infty, !in2infty, and OPENSSL_ia32cap_P copy.
1631 &stack_push(8*15+3);
1633 &call ("_picup_eax");
1635 &picmeup("edx","OPENSSL_ia32cap_P","eax",&label("pic"));
1636 &mov ("ebp",&DWP(0,"edx")); }
1638 &lea ("edi",&DWP($in1_x,"esp"));
1639 for($i=0;$i<96;$i+=16) {
1640 &mov ("eax",&DWP($i+0,"esi")); # copy in1
1641 &mov ("ebx",&DWP($i+4,"esi"));
1642 &mov ("ecx",&DWP($i+8,"esi"));
1643 &mov ("edx",&DWP($i+12,"esi"));
1644 &mov (&DWP($i+0,"edi"),"eax");
1645 &mov (&DWP(32*15+8,"esp"),"ebp") if ($i==0);
1646 &mov ("ebp","eax") if ($i==0);
1647 &or ("ebp","eax") if ($i!=0 && $i<64);
1648 &mov (&DWP($i+4,"edi"),"ebx");
1649 &or ("ebp","ebx") if ($i<64);
1650 &mov (&DWP($i+8,"edi"),"ecx");
1651 &or ("ebp","ecx") if ($i<64);
1652 &mov (&DWP($i+12,"edi"),"edx");
1653 &or ("ebp","edx") if ($i<64);
1656 &mov ("esi",&wparam(2));
1660 &mov (&DWP(32*15+0,"esp"),"ebp"); # !in1infty
1662 &lea ("edi",&DWP($in2_x,"esp"));
1663 for($i=0;$i<64;$i+=16) {
1664 &mov ("eax",&DWP($i+0,"esi")); # copy in2
1665 &mov ("ebx",&DWP($i+4,"esi"));
1666 &mov ("ecx",&DWP($i+8,"esi"));
1667 &mov ("edx",&DWP($i+12,"esi"));
1668 &mov (&DWP($i+0,"edi"),"eax");
1669 &mov ("ebp","eax") if ($i==0);
1670 &or ("ebp","eax") if ($i!=0);
1671 &mov (&DWP($i+4,"edi"),"ebx");
1673 &mov (&DWP($i+8,"edi"),"ecx");
1675 &mov (&DWP($i+12,"edi"),"edx");
1679 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1681 &lea ("esi",&DWP($in1_z,"esp"));
1683 &lea ("ebp",&DWP($in1_z,"esp"));
1685 &lea ("edi",&DWP($Z1sqr,"esp"));
1686 &mov (&DWP(32*15+4,"esp"),"ebx"); # !in2infty
1688 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Z1sqr, in1_z);
1690 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1691 &lea ("esi",&DWP($in2_x,"esp"));
1692 &mov ("ebp","edi"); # %esi is stull &Z1sqr
1693 &lea ("edi",&DWP($U2,"esp"));
1694 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, Z1sqr, in2_x);
1696 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1697 &lea ("esi",&DWP($in1_z,"esp"));
1698 &lea ("ebp",&DWP($Z1sqr,"esp"));
1699 &lea ("edi",&DWP($S2,"esp"));
1700 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Z1sqr, in1_z);
1702 &lea ("esi",&DWP($U2,"esp"));
1703 &lea ("ebp",&DWP($in1_x,"esp"));
1704 &lea ("edi",&DWP($H,"esp"));
1705 &call ("_ecp_nistz256_sub"); # p256_sub(H, U2, in1_x);
1707 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1708 &lea ("esi",&DWP($in2_y,"esp"));
1709 &lea ("ebp",&DWP($S2,"esp"));
1710 &lea ("edi",&DWP($S2,"esp"));
1711 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, S2, in2_y);
1713 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1714 &lea ("esi",&DWP($in1_z,"esp"));
1715 &lea ("ebp",&DWP($H,"esp"));
1716 &lea ("edi",&DWP($res_z,"esp"));
1717 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_z, H, in1_z);
1719 &lea ("esi",&DWP($S2,"esp"));
1720 &lea ("ebp",&DWP($in1_y,"esp"));
1721 &lea ("edi",&DWP($R,"esp"));
1722 &call ("_ecp_nistz256_sub"); # p256_sub(R, S2, in1_y);
1724 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1725 &lea ("esi",&DWP($H,"esp"));
1726 &lea ("ebp",&DWP($H,"esp"));
1727 &lea ("edi",&DWP($Hsqr,"esp"));
1728 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Hsqr, H);
1730 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1731 &lea ("esi",&DWP($R,"esp"));
1732 &lea ("ebp",&DWP($R,"esp"));
1733 &lea ("edi",&DWP($Rsqr,"esp"));
1734 &call ("_ecp_nistz256_mul_mont"); # p256_sqr_mont(Rsqr, R);
1736 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1737 &lea ("esi",&DWP($in1_x,"esp"));
1738 &lea ("ebp",&DWP($Hsqr,"esp"));
1739 &lea ("edi",&DWP($U2,"esp"));
1740 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(U2, in1_x, Hsqr);
1742 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1743 &lea ("esi",&DWP($H,"esp"));
1744 &lea ("ebp",&DWP($Hsqr,"esp"));
1745 &lea ("edi",&DWP($Hcub,"esp"));
1746 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(Hcub, Hsqr, H);
1748 &lea ("esi",&DWP($U2,"esp"));
1749 &lea ("ebp",&DWP($U2,"esp"));
1750 &lea ("edi",&DWP($Hsqr,"esp"));
1751 &call ("_ecp_nistz256_add"); # p256_mul_by_2(Hsqr, U2);
1753 &lea ("esi",&DWP($Rsqr,"esp"));
1754 &lea ("ebp",&DWP($Hsqr,"esp"));
1755 &lea ("edi",&DWP($res_x,"esp"));
1756 &call ("_ecp_nistz256_sub"); # p256_sub(res_x, Rsqr, Hsqr);
1758 &lea ("esi",&DWP($res_x,"esp"));
1759 &lea ("ebp",&DWP($Hcub,"esp"));
1760 &lea ("edi",&DWP($res_x,"esp"));
1761 &call ("_ecp_nistz256_sub"); # p256_sub(res_x, res_x, Hcub);
1763 &lea ("esi",&DWP($U2,"esp"));
1764 &lea ("ebp",&DWP($res_x,"esp"));
1765 &lea ("edi",&DWP($res_y,"esp"));
1766 &call ("_ecp_nistz256_sub"); # p256_sub(res_y, U2, res_x);
1768 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1769 &lea ("esi",&DWP($Hcub,"esp"));
1770 &lea ("ebp",&DWP($in1_y,"esp"));
1771 &lea ("edi",&DWP($S2,"esp"));
1772 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(S2, Hcub, in1_y);
1774 &mov ("eax",&DWP(32*15+8,"esp")); # OPENSSL_ia32cap_P copy
1775 &lea ("esi",&DWP($R,"esp"));
1776 &lea ("ebp",&DWP($res_y,"esp"));
1777 &lea ("edi",&DWP($res_y,"esp"));
1778 &call ("_ecp_nistz256_mul_mont"); # p256_mul_mont(res_y, res_y, R);
1780 &lea ("esi",&DWP($res_y,"esp"));
1781 &lea ("ebp",&DWP($S2,"esp"));
1782 &lea ("edi",&DWP($res_y,"esp"));
1783 &call ("_ecp_nistz256_sub"); # p256_sub(res_y, res_y, S2);
1785 &mov ("ebp",&DWP(32*15+0,"esp")); # !in1infty
1786 &mov ("esi",&DWP(32*15+4,"esp")); # !in2infty
1787 &mov ("edi",&wparam(0));
1794 ########################################
1796 for($i=64;$i<96;$i+=4) {
1797 my $one=@ONE_mont[($i-64)/4];
1800 &and ("eax",&DWP($res_x+$i,"esp"));
1801 &mov ("ebx","ebp") if ($one && $one!=-1);
1802 &and ("ebx",$one) if ($one && $one!=-1);
1804 &and ("ecx",&DWP($in1_x+$i,"esp"));
1805 &or ("eax",$one==-1?"ebp":"ebx") if ($one);
1807 &mov (&DWP($i,"edi"),"eax");
1809 for($i=0;$i<64;$i+=4) {
1811 &and ("eax",&DWP($res_x+$i,"esp"));
1813 &and ("ebx",&DWP($in2_x+$i,"esp"));
1815 &and ("ecx",&DWP($in1_x+$i,"esp"));
1818 &mov (&DWP($i,"edi"),"eax");
1821 } &function_end("ecp_nistz256_point_add_affine");