Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bn/asm/rsaz-3k-avx512.pl

raw
1 # Copyright 2021-2024 The OpenSSL Project Authors. All Rights Reserved.
2 # Copyright (c) 2021, Intel Corporation. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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 #
9 #
10 # Originally written by Sergey Kirillov and Andrey Matyukov
11 # Intel Corporation
12 #
13 # March 2021
14 #
15 # Initial release.
16 #
17 # Implementation utilizes 256-bit (ymm) registers to avoid frequency scaling issues.
18 #
19 # IceLake-Client @ 1.3GHz
20 # |---------+-----------------------+---------------+-------------|
21 # | | OpenSSL 3.0.0-alpha15 | this | Unit |
22 # |---------+-----------------------+---------------+-------------|
23 # | rsa3072 | 6 397 637 | 2 866 593 | cycles/sign |
24 # | | 203.2 | 453.5 / +123% | sign/s |
25 # |---------+-----------------------+---------------+-------------|
26 #
27
28 # $output is the last argument if it looks like a file (it has an extension)
29 # $flavour is the first argument if it doesn't look like a file
30 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
31 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
32
33 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
34 $avx512ifma=0;
35
36 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
37 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
38 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
39 die "can't locate x86_64-xlate.pl";
40
41 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
42 =~ /GNU assembler version ([0-9]+)\.([0-9]+)/) {
43 my $ver = $1 + $2/100.0; # 3.1->3.01, 3.10->3.10
44 $avx512ifma = ($ver >= 2.26);
45 }
46
47 if (!$avx512ifma && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
48 `nasm -v 2>&1` =~ /NASM version ([0-9]+)\.([0-9]+)(?:\.([0-9]+))?/) {
49 my $ver = $1 + $2/100.0 + $3/10000.0; # 3.1.0->3.01, 3.10.1->3.1001
50 $avx512ifma = ($ver >= 2.1108);
51 }
52
53 if (!$avx512ifma && `$ENV{CC} -v 2>&1`
54 =~ /(Apple)?\s*((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)\.([0-9]+)?/) {
55 my $ver = $3 + $4/100.0 + $5/10000.0; # 3.1.0->3.01, 3.10.1->3.1001
56 if ($1) {
57 # Apple conditions, they use a different version series, see
58 # https://en.wikipedia.org/wiki/Xcode#Xcode_7.0_-_10.x_(since_Free_On-Device_Development)_2
59 # clang 7.0.0 is Apple clang 10.0.1
60 $avx512ifma = ($ver>=10.0001)
61 } else {
62 $avx512ifma = ($ver>=7.0);
63 }
64 }
65
66 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
67 or die "can't call $xlate: $!";
68 *STDOUT=*OUT;
69
70 if ($avx512ifma>0) {{{
71 @_6_args_universal_ABI = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
72
73 ###############################################################################
74 # Almost Montgomery Multiplication (AMM) for 30-digit number in radix 2^52.
75 #
76 # AMM is defined as presented in the paper [1].
77 #
78 # The input and output are presented in 2^52 radix domain, i.e.
79 # |res|, |a|, |b|, |m| are arrays of 32 64-bit qwords with 12 high bits zeroed
80 #
81 # NOTE: the function uses zero-padded data - 2 high QWs is a padding.
82 #
83 # |k0| is a Montgomery coefficient, which is here k0 = -1/m mod 2^64
84 #
85 # NB: the AMM implementation does not perform "conditional" subtraction step
86 # specified in the original algorithm as according to the Lemma 1 from the paper
87 # [2], the result will be always < 2*m and can be used as a direct input to
88 # the next AMM iteration. This post-condition is true, provided the correct
89 # parameter |s| (notion of the Lemma 1 from [2]) is chosen, i.e. s >= n + 2 * k,
90 # which matches our case: 1560 > 1536 + 2 * 1.
91 #
92 # [1] Gueron, S. Efficient software implementations of modular exponentiation.
93 # DOI: 10.1007/s13389-012-0031-5
94 # [2] Gueron, S. Enhanced Montgomery Multiplication.
95 # DOI: 10.1007/3-540-36400-5_5
96 #
97 # void ossl_rsaz_amm52x30_x1_ifma256(BN_ULONG *res,
98 # const BN_ULONG *a,
99 # const BN_ULONG *b,
100 # const BN_ULONG *m,
101 # BN_ULONG k0);
102 ###############################################################################
103 {
104 # input parameters ("%rdi","%rsi","%rdx","%rcx","%r8")
105 my ($res,$a,$b,$m,$k0) = @_6_args_universal_ABI;
106
107 my $mask52 = "%rax";
108 my $acc0_0 = "%r9";
109 my $acc0_0_low = "%r9d";
110 my $acc0_1 = "%r15";
111 my $acc0_1_low = "%r15d";
112 my $b_ptr = "%r11";
113
114 my $iter = "%ebx";
115
116 my $zero = "%ymm0";
117 my $Bi = "%ymm1";
118 my $Yi = "%ymm2";
119 my ($R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h) = map("%ymm$_",(3..10));
120 my ($R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h) = map("%ymm$_",(11..18));
121
122 # Registers mapping for normalization
123 my ($T0,$T0h,$T1,$T1h,$T2,$T2h,$T3,$T3h) = ("$zero", "$Bi", "$Yi", map("%ymm$_", (19..23)));
124
125 sub amm52x30_x1() {
126 # _data_offset - offset in the |a| or |m| arrays pointing to the beginning
127 # of data for corresponding AMM operation;
128 # _b_offset - offset in the |b| array pointing to the next qword digit;
129 my ($_data_offset,$_b_offset,$_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_R2h,$_R3,$_R3h,$_k0) = @_;
130 my $_R0_xmm = $_R0;
131 $_R0_xmm =~ s/%y/%x/;
132 $code.=<<___;
133 movq $_b_offset($b_ptr), %r13 # b[i]
134
135 vpbroadcastq %r13, $Bi # broadcast b[i]
136 movq $_data_offset($a), %rdx
137 mulx %r13, %r13, %r12 # a[0]*b[i] = (t0,t2)
138 addq %r13, $_acc # acc += t0
139 movq %r12, %r10
140 adcq \$0, %r10 # t2 += CF
141
142 movq $_k0, %r13
143 imulq $_acc, %r13 # acc * k0
144 andq $mask52, %r13 # yi = (acc * k0) & mask52
145
146 vpbroadcastq %r13, $Yi # broadcast y[i]
147 movq $_data_offset($m), %rdx
148 mulx %r13, %r13, %r12 # yi * m[0] = (t0,t1)
149 addq %r13, $_acc # acc += t0
150 adcq %r12, %r10 # t2 += (t1 + CF)
151
152 shrq \$52, $_acc
153 salq \$12, %r10
154 or %r10, $_acc # acc = ((acc >> 52) | (t2 << 12))
155
156 vpmadd52luq `$_data_offset+64*0`($a), $Bi, $_R0
157 vpmadd52luq `$_data_offset+64*0+32`($a), $Bi, $_R0h
158 vpmadd52luq `$_data_offset+64*1`($a), $Bi, $_R1
159 vpmadd52luq `$_data_offset+64*1+32`($a), $Bi, $_R1h
160 vpmadd52luq `$_data_offset+64*2`($a), $Bi, $_R2
161 vpmadd52luq `$_data_offset+64*2+32`($a), $Bi, $_R2h
162 vpmadd52luq `$_data_offset+64*3`($a), $Bi, $_R3
163 vpmadd52luq `$_data_offset+64*3+32`($a), $Bi, $_R3h
164
165 vpmadd52luq `$_data_offset+64*0`($m), $Yi, $_R0
166 vpmadd52luq `$_data_offset+64*0+32`($m), $Yi, $_R0h
167 vpmadd52luq `$_data_offset+64*1`($m), $Yi, $_R1
168 vpmadd52luq `$_data_offset+64*1+32`($m), $Yi, $_R1h
169 vpmadd52luq `$_data_offset+64*2`($m), $Yi, $_R2
170 vpmadd52luq `$_data_offset+64*2+32`($m), $Yi, $_R2h
171 vpmadd52luq `$_data_offset+64*3`($m), $Yi, $_R3
172 vpmadd52luq `$_data_offset+64*3+32`($m), $Yi, $_R3h
173
174 # Shift accumulators right by 1 qword, zero extending the highest one
175 valignq \$1, $_R0, $_R0h, $_R0
176 valignq \$1, $_R0h, $_R1, $_R0h
177 valignq \$1, $_R1, $_R1h, $_R1
178 valignq \$1, $_R1h, $_R2, $_R1h
179 valignq \$1, $_R2, $_R2h, $_R2
180 valignq \$1, $_R2h, $_R3, $_R2h
181 valignq \$1, $_R3, $_R3h, $_R3
182 valignq \$1, $_R3h, $zero, $_R3h
183
184 vmovq $_R0_xmm, %r13
185 addq %r13, $_acc # acc += R0[0]
186
187 vpmadd52huq `$_data_offset+64*0`($a), $Bi, $_R0
188 vpmadd52huq `$_data_offset+64*0+32`($a), $Bi, $_R0h
189 vpmadd52huq `$_data_offset+64*1`($a), $Bi, $_R1
190 vpmadd52huq `$_data_offset+64*1+32`($a), $Bi, $_R1h
191 vpmadd52huq `$_data_offset+64*2`($a), $Bi, $_R2
192 vpmadd52huq `$_data_offset+64*2+32`($a), $Bi, $_R2h
193 vpmadd52huq `$_data_offset+64*3`($a), $Bi, $_R3
194 vpmadd52huq `$_data_offset+64*3+32`($a), $Bi, $_R3h
195
196 vpmadd52huq `$_data_offset+64*0`($m), $Yi, $_R0
197 vpmadd52huq `$_data_offset+64*0+32`($m), $Yi, $_R0h
198 vpmadd52huq `$_data_offset+64*1`($m), $Yi, $_R1
199 vpmadd52huq `$_data_offset+64*1+32`($m), $Yi, $_R1h
200 vpmadd52huq `$_data_offset+64*2`($m), $Yi, $_R2
201 vpmadd52huq `$_data_offset+64*2+32`($m), $Yi, $_R2h
202 vpmadd52huq `$_data_offset+64*3`($m), $Yi, $_R3
203 vpmadd52huq `$_data_offset+64*3+32`($m), $Yi, $_R3h
204 ___
205 }
206
207 # Normalization routine: handles carry bits and gets bignum qwords to normalized
208 # 2^52 representation.
209 #
210 # Uses %r8-14,%e[abcd]x
211 sub amm52x30_x1_norm {
212 my ($_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_R2h,$_R3,$_R3h) = @_;
213 $code.=<<___;
214 # Put accumulator to low qword in R0
215 vpbroadcastq $_acc, $T0
216 vpblendd \$3, $T0, $_R0, $_R0
217
218 # Extract "carries" (12 high bits) from each QW of the bignum
219 # Save them to LSB of QWs in T0..Tn
220 vpsrlq \$52, $_R0, $T0
221 vpsrlq \$52, $_R0h, $T0h
222 vpsrlq \$52, $_R1, $T1
223 vpsrlq \$52, $_R1h, $T1h
224 vpsrlq \$52, $_R2, $T2
225 vpsrlq \$52, $_R2h, $T2h
226 vpsrlq \$52, $_R3, $T3
227 vpsrlq \$52, $_R3h, $T3h
228
229 # "Shift left" T0..Tn by 1 QW
230 valignq \$3, $T3, $T3h, $T3h
231 valignq \$3, $T2h, $T3, $T3
232 valignq \$3, $T2, $T2h, $T2h
233 valignq \$3, $T1h, $T2, $T2
234 valignq \$3, $T1, $T1h, $T1h
235 valignq \$3, $T0h, $T1, $T1
236 valignq \$3, $T0, $T0h, $T0h
237 valignq \$3, .Lzeros(%rip), $T0, $T0
238
239 # Drop "carries" from R0..Rn QWs
240 vpandq .Lmask52x4(%rip), $_R0, $_R0
241 vpandq .Lmask52x4(%rip), $_R0h, $_R0h
242 vpandq .Lmask52x4(%rip), $_R1, $_R1
243 vpandq .Lmask52x4(%rip), $_R1h, $_R1h
244 vpandq .Lmask52x4(%rip), $_R2, $_R2
245 vpandq .Lmask52x4(%rip), $_R2h, $_R2h
246 vpandq .Lmask52x4(%rip), $_R3, $_R3
247 vpandq .Lmask52x4(%rip), $_R3h, $_R3h
248
249 # Sum R0..Rn with corresponding adjusted carries
250 vpaddq $T0, $_R0, $_R0
251 vpaddq $T0h, $_R0h, $_R0h
252 vpaddq $T1, $_R1, $_R1
253 vpaddq $T1h, $_R1h, $_R1h
254 vpaddq $T2, $_R2, $_R2
255 vpaddq $T2h, $_R2h, $_R2h
256 vpaddq $T3, $_R3, $_R3
257 vpaddq $T3h, $_R3h, $_R3h
258
259 # Now handle carry bits from this addition
260 # Get mask of QWs whose 52-bit parts overflow
261 vpcmpuq \$6,.Lmask52x4(%rip),${_R0},%k1 # OP=nle (i.e. gt)
262 vpcmpuq \$6,.Lmask52x4(%rip),${_R0h},%k2
263 kmovb %k1,%r14d
264 kmovb %k2,%r13d
265 shl \$4,%r13b
266 or %r13b,%r14b
267
268 vpcmpuq \$6,.Lmask52x4(%rip),${_R1},%k1
269 vpcmpuq \$6,.Lmask52x4(%rip),${_R1h},%k2
270 kmovb %k1,%r13d
271 kmovb %k2,%r12d
272 shl \$4,%r12b
273 or %r12b,%r13b
274
275 vpcmpuq \$6,.Lmask52x4(%rip),${_R2},%k1
276 vpcmpuq \$6,.Lmask52x4(%rip),${_R2h},%k2
277 kmovb %k1,%r12d
278 kmovb %k2,%r11d
279 shl \$4,%r11b
280 or %r11b,%r12b
281
282 vpcmpuq \$6,.Lmask52x4(%rip),${_R3},%k1
283 vpcmpuq \$6,.Lmask52x4(%rip),${_R3h},%k2
284 kmovb %k1,%r11d
285 kmovb %k2,%r10d
286 shl \$4,%r10b
287 or %r10b,%r11b
288
289 addb %r14b,%r14b
290 adcb %r13b,%r13b
291 adcb %r12b,%r12b
292 adcb %r11b,%r11b
293
294 # Get mask of QWs whose 52-bit parts saturated
295 vpcmpuq \$0,.Lmask52x4(%rip),${_R0},%k1 # OP=eq
296 vpcmpuq \$0,.Lmask52x4(%rip),${_R0h},%k2
297 kmovb %k1,%r9d
298 kmovb %k2,%r8d
299 shl \$4,%r8b
300 or %r8b,%r9b
301
302 vpcmpuq \$0,.Lmask52x4(%rip),${_R1},%k1
303 vpcmpuq \$0,.Lmask52x4(%rip),${_R1h},%k2
304 kmovb %k1,%r8d
305 kmovb %k2,%edx
306 shl \$4,%dl
307 or %dl,%r8b
308
309 vpcmpuq \$0,.Lmask52x4(%rip),${_R2},%k1
310 vpcmpuq \$0,.Lmask52x4(%rip),${_R2h},%k2
311 kmovb %k1,%edx
312 kmovb %k2,%ecx
313 shl \$4,%cl
314 or %cl,%dl
315
316 vpcmpuq \$0,.Lmask52x4(%rip),${_R3},%k1
317 vpcmpuq \$0,.Lmask52x4(%rip),${_R3h},%k2
318 kmovb %k1,%ecx
319 kmovb %k2,%ebx
320 shl \$4,%bl
321 or %bl,%cl
322
323 addb %r9b,%r14b
324 adcb %r8b,%r13b
325 adcb %dl,%r12b
326 adcb %cl,%r11b
327
328 xor %r9b,%r14b
329 xor %r8b,%r13b
330 xor %dl,%r12b
331 xor %cl,%r11b
332
333 kmovb %r14d,%k1
334 shr \$4,%r14b
335 kmovb %r14d,%k2
336 kmovb %r13d,%k3
337 shr \$4,%r13b
338 kmovb %r13d,%k4
339 kmovb %r12d,%k5
340 shr \$4,%r12b
341 kmovb %r12d,%k6
342 kmovb %r11d,%k7
343
344 vpsubq .Lmask52x4(%rip), $_R0, ${_R0}{%k1}
345 vpsubq .Lmask52x4(%rip), $_R0h, ${_R0h}{%k2}
346 vpsubq .Lmask52x4(%rip), $_R1, ${_R1}{%k3}
347 vpsubq .Lmask52x4(%rip), $_R1h, ${_R1h}{%k4}
348 vpsubq .Lmask52x4(%rip), $_R2, ${_R2}{%k5}
349 vpsubq .Lmask52x4(%rip), $_R2h, ${_R2h}{%k6}
350 vpsubq .Lmask52x4(%rip), $_R3, ${_R3}{%k7}
351
352 vpandq .Lmask52x4(%rip), $_R0, $_R0
353 vpandq .Lmask52x4(%rip), $_R0h, $_R0h
354 vpandq .Lmask52x4(%rip), $_R1, $_R1
355 vpandq .Lmask52x4(%rip), $_R1h, $_R1h
356 vpandq .Lmask52x4(%rip), $_R2, $_R2
357 vpandq .Lmask52x4(%rip), $_R2h, $_R2h
358 vpandq .Lmask52x4(%rip), $_R3, $_R3
359
360 shr \$4,%r11b
361 kmovb %r11d,%k1
362
363 vpsubq .Lmask52x4(%rip), $_R3h, ${_R3h}{%k1}
364
365 vpandq .Lmask52x4(%rip), $_R3h, $_R3h
366 ___
367 }
368
369 $code.=<<___;
370 .text
371
372 .globl ossl_rsaz_amm52x30_x1_ifma256
373 .type ossl_rsaz_amm52x30_x1_ifma256,\@function,5
374 .align 32
375 ossl_rsaz_amm52x30_x1_ifma256:
376 .cfi_startproc
377 endbranch
378 push %rbx
379 .cfi_push %rbx
380 push %rbp
381 .cfi_push %rbp
382 push %r12
383 .cfi_push %r12
384 push %r13
385 .cfi_push %r13
386 push %r14
387 .cfi_push %r14
388 push %r15
389 .cfi_push %r15
390 ___
391 $code.=<<___ if ($win64);
392 lea -168(%rsp),%rsp # 16*10 + (8 bytes to get correct 16-byte SIMD alignment)
393 vmovdqa64 %xmm6, `0*16`(%rsp) # save non-volatile registers
394 vmovdqa64 %xmm7, `1*16`(%rsp)
395 vmovdqa64 %xmm8, `2*16`(%rsp)
396 vmovdqa64 %xmm9, `3*16`(%rsp)
397 vmovdqa64 %xmm10,`4*16`(%rsp)
398 vmovdqa64 %xmm11,`5*16`(%rsp)
399 vmovdqa64 %xmm12,`6*16`(%rsp)
400 vmovdqa64 %xmm13,`7*16`(%rsp)
401 vmovdqa64 %xmm14,`8*16`(%rsp)
402 vmovdqa64 %xmm15,`9*16`(%rsp)
403 .Lossl_rsaz_amm52x30_x1_ifma256_body:
404 ___
405 $code.=<<___;
406 # Zeroing accumulators
407 vpxord $zero, $zero, $zero
408 vmovdqa64 $zero, $R0_0
409 vmovdqa64 $zero, $R0_0h
410 vmovdqa64 $zero, $R1_0
411 vmovdqa64 $zero, $R1_0h
412 vmovdqa64 $zero, $R2_0
413 vmovdqa64 $zero, $R2_0h
414 vmovdqa64 $zero, $R3_0
415 vmovdqa64 $zero, $R3_0h
416
417 xorl $acc0_0_low, $acc0_0_low
418
419 movq $b, $b_ptr # backup address of b
420 movq \$0xfffffffffffff, $mask52 # 52-bit mask
421
422 # Loop over 30 digits unrolled by 4
423 mov \$7, $iter
424
425 .align 32
426 .Lloop7:
427 ___
428 foreach my $idx (0..3) {
429 &amm52x30_x1(0,8*$idx,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
430 }
431 $code.=<<___;
432 lea `4*8`($b_ptr), $b_ptr
433 dec $iter
434 jne .Lloop7
435 ___
436 &amm52x30_x1(0,8*0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
437 &amm52x30_x1(0,8*1,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,$k0);
438
439 &amm52x30_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h);
440 $code.=<<___;
441
442 vmovdqu64 $R0_0, `0*32`($res)
443 vmovdqu64 $R0_0h, `1*32`($res)
444 vmovdqu64 $R1_0, `2*32`($res)
445 vmovdqu64 $R1_0h, `3*32`($res)
446 vmovdqu64 $R2_0, `4*32`($res)
447 vmovdqu64 $R2_0h, `5*32`($res)
448 vmovdqu64 $R3_0, `6*32`($res)
449 vmovdqu64 $R3_0h, `7*32`($res)
450
451 vzeroupper
452 lea (%rsp),%rax
453 .cfi_def_cfa_register %rax
454 ___
455 $code.=<<___ if ($win64);
456 vmovdqa64 `0*16`(%rax),%xmm6
457 vmovdqa64 `1*16`(%rax),%xmm7
458 vmovdqa64 `2*16`(%rax),%xmm8
459 vmovdqa64 `3*16`(%rax),%xmm9
460 vmovdqa64 `4*16`(%rax),%xmm10
461 vmovdqa64 `5*16`(%rax),%xmm11
462 vmovdqa64 `6*16`(%rax),%xmm12
463 vmovdqa64 `7*16`(%rax),%xmm13
464 vmovdqa64 `8*16`(%rax),%xmm14
465 vmovdqa64 `9*16`(%rax),%xmm15
466 lea 168(%rsp),%rax
467 ___
468 $code.=<<___;
469 mov 0(%rax),%r15
470 .cfi_restore %r15
471 mov 8(%rax),%r14
472 .cfi_restore %r14
473 mov 16(%rax),%r13
474 .cfi_restore %r13
475 mov 24(%rax),%r12
476 .cfi_restore %r12
477 mov 32(%rax),%rbp
478 .cfi_restore %rbp
479 mov 40(%rax),%rbx
480 .cfi_restore %rbx
481 lea 48(%rax),%rsp # restore rsp
482 .cfi_def_cfa %rsp,8
483 .Lossl_rsaz_amm52x30_x1_ifma256_epilogue:
484 ret
485 .cfi_endproc
486 .size ossl_rsaz_amm52x30_x1_ifma256, .-ossl_rsaz_amm52x30_x1_ifma256
487 ___
488
489 $code.=<<___;
490 .section .rodata align=32
491 .align 32
492 .Lmask52x4:
493 .quad 0xfffffffffffff
494 .quad 0xfffffffffffff
495 .quad 0xfffffffffffff
496 .quad 0xfffffffffffff
497 ___
498
499 ###############################################################################
500 # Dual Almost Montgomery Multiplication for 30-digit number in radix 2^52
501 #
502 # See description of ossl_rsaz_amm52x30_x1_ifma256() above for details about Almost
503 # Montgomery Multiplication algorithm and function input parameters description.
504 #
505 # This function does two AMMs for two independent inputs, hence dual.
506 #
507 # NOTE: the function uses zero-padded data - 2 high QWs is a padding.
508 #
509 # void ossl_rsaz_amm52x30_x2_ifma256(BN_ULONG out[2][32],
510 # const BN_ULONG a[2][32],
511 # const BN_ULONG b[2][32],
512 # const BN_ULONG m[2][32],
513 # const BN_ULONG k0[2]);
514 ###############################################################################
515
516 $code.=<<___;
517 .text
518
519 .globl ossl_rsaz_amm52x30_x2_ifma256
520 .type ossl_rsaz_amm52x30_x2_ifma256,\@function,5
521 .align 32
522 ossl_rsaz_amm52x30_x2_ifma256:
523 .cfi_startproc
524 endbranch
525 push %rbx
526 .cfi_push %rbx
527 push %rbp
528 .cfi_push %rbp
529 push %r12
530 .cfi_push %r12
531 push %r13
532 .cfi_push %r13
533 push %r14
534 .cfi_push %r14
535 push %r15
536 .cfi_push %r15
537 ___
538 $code.=<<___ if ($win64);
539 lea -168(%rsp),%rsp
540 vmovdqa64 %xmm6, `0*16`(%rsp) # save non-volatile registers
541 vmovdqa64 %xmm7, `1*16`(%rsp)
542 vmovdqa64 %xmm8, `2*16`(%rsp)
543 vmovdqa64 %xmm9, `3*16`(%rsp)
544 vmovdqa64 %xmm10,`4*16`(%rsp)
545 vmovdqa64 %xmm11,`5*16`(%rsp)
546 vmovdqa64 %xmm12,`6*16`(%rsp)
547 vmovdqa64 %xmm13,`7*16`(%rsp)
548 vmovdqa64 %xmm14,`8*16`(%rsp)
549 vmovdqa64 %xmm15,`9*16`(%rsp)
550 .Lossl_rsaz_amm52x30_x2_ifma256_body:
551 ___
552 $code.=<<___;
553 # Zeroing accumulators
554 vpxord $zero, $zero, $zero
555 vmovdqa64 $zero, $R0_0
556 vmovdqa64 $zero, $R0_0h
557 vmovdqa64 $zero, $R1_0
558 vmovdqa64 $zero, $R1_0h
559 vmovdqa64 $zero, $R2_0
560 vmovdqa64 $zero, $R2_0h
561 vmovdqa64 $zero, $R3_0
562 vmovdqa64 $zero, $R3_0h
563
564 vmovdqa64 $zero, $R0_1
565 vmovdqa64 $zero, $R0_1h
566 vmovdqa64 $zero, $R1_1
567 vmovdqa64 $zero, $R1_1h
568 vmovdqa64 $zero, $R2_1
569 vmovdqa64 $zero, $R2_1h
570 vmovdqa64 $zero, $R3_1
571 vmovdqa64 $zero, $R3_1h
572
573
574 xorl $acc0_0_low, $acc0_0_low
575 xorl $acc0_1_low, $acc0_1_low
576
577 movq $b, $b_ptr # backup address of b
578 movq \$0xfffffffffffff, $mask52 # 52-bit mask
579
580 mov \$30, $iter
581
582 .align 32
583 .Lloop30:
584 ___
585 &amm52x30_x1( 0, 0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h,"($k0)");
586 # 32*8 = offset of the next dimension in two-dimension array
587 &amm52x30_x1(32*8,32*8,$acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h,"8($k0)");
588 $code.=<<___;
589 lea 8($b_ptr), $b_ptr
590 dec $iter
591 jne .Lloop30
592 ___
593 &amm52x30_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$R2_0h,$R3_0,$R3_0h);
594 &amm52x30_x1_norm($acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,$R2_1h,$R3_1,$R3_1h);
595 $code.=<<___;
596
597 vmovdqu64 $R0_0, `0*32`($res)
598 vmovdqu64 $R0_0h, `1*32`($res)
599 vmovdqu64 $R1_0, `2*32`($res)
600 vmovdqu64 $R1_0h, `3*32`($res)
601 vmovdqu64 $R2_0, `4*32`($res)
602 vmovdqu64 $R2_0h, `5*32`($res)
603 vmovdqu64 $R3_0, `6*32`($res)
604 vmovdqu64 $R3_0h, `7*32`($res)
605
606 vmovdqu64 $R0_1, `8*32`($res)
607 vmovdqu64 $R0_1h, `9*32`($res)
608 vmovdqu64 $R1_1, `10*32`($res)
609 vmovdqu64 $R1_1h, `11*32`($res)
610 vmovdqu64 $R2_1, `12*32`($res)
611 vmovdqu64 $R2_1h, `13*32`($res)
612 vmovdqu64 $R3_1, `14*32`($res)
613 vmovdqu64 $R3_1h, `15*32`($res)
614
615 vzeroupper
616 lea (%rsp),%rax
617 .cfi_def_cfa_register %rax
618 ___
619 $code.=<<___ if ($win64);
620 vmovdqa64 `0*16`(%rax),%xmm6
621 vmovdqa64 `1*16`(%rax),%xmm7
622 vmovdqa64 `2*16`(%rax),%xmm8
623 vmovdqa64 `3*16`(%rax),%xmm9
624 vmovdqa64 `4*16`(%rax),%xmm10
625 vmovdqa64 `5*16`(%rax),%xmm11
626 vmovdqa64 `6*16`(%rax),%xmm12
627 vmovdqa64 `7*16`(%rax),%xmm13
628 vmovdqa64 `8*16`(%rax),%xmm14
629 vmovdqa64 `9*16`(%rax),%xmm15
630 lea 168(%rsp),%rax
631 ___
632 $code.=<<___;
633 mov 0(%rax),%r15
634 .cfi_restore %r15
635 mov 8(%rax),%r14
636 .cfi_restore %r14
637 mov 16(%rax),%r13
638 .cfi_restore %r13
639 mov 24(%rax),%r12
640 .cfi_restore %r12
641 mov 32(%rax),%rbp
642 .cfi_restore %rbp
643 mov 40(%rax),%rbx
644 .cfi_restore %rbx
645 lea 48(%rax),%rsp
646 .cfi_def_cfa %rsp,8
647 .Lossl_rsaz_amm52x30_x2_ifma256_epilogue:
648 ret
649 .cfi_endproc
650 .size ossl_rsaz_amm52x30_x2_ifma256, .-ossl_rsaz_amm52x30_x2_ifma256
651 ___
652 }
653
654 ###############################################################################
655 # Constant time extraction from the precomputed table of powers base^i, where
656 # i = 0..2^EXP_WIN_SIZE-1
657 #
658 # The input |red_table| contains precomputations for two independent base values.
659 # |red_table_idx1| and |red_table_idx2| are corresponding power indexes.
660 #
661 # Extracted value (output) is 2 (30 + 2) digits numbers in 2^52 radix.
662 # (2 high QW is zero padding)
663 #
664 # void ossl_extract_multiplier_2x30_win5(BN_ULONG *red_Y,
665 # const BN_ULONG red_table[1 << EXP_WIN_SIZE][2][32],
666 # int red_table_idx1, int red_table_idx2);
667 #
668 # EXP_WIN_SIZE = 5
669 ###############################################################################
670 {
671 # input parameters
672 my ($out,$red_tbl,$red_tbl_idx1,$red_tbl_idx2)=$win64 ? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
673 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
674
675 my ($t0,$t1,$t2,$t3,$t4,$t5) = map("%ymm$_", (0..5));
676 my ($t6,$t7,$t8,$t9,$t10,$t11,$t12,$t13,$t14,$t15) = map("%ymm$_", (16..25));
677 my ($tmp,$cur_idx,$idx1,$idx2,$ones) = map("%ymm$_", (26..30));
678
679 my @t = ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10,$t11,$t12,$t13,$t14,$t15);
680 my $t0xmm = $t0;
681 $t0xmm =~ s/%y/%x/;
682
683 $code.=<<___;
684 .text
685
686 .align 32
687 .globl ossl_extract_multiplier_2x30_win5
688 .type ossl_extract_multiplier_2x30_win5,\@abi-omnipotent
689 ossl_extract_multiplier_2x30_win5:
690 .cfi_startproc
691 endbranch
692 vmovdqa64 .Lones(%rip), $ones # broadcast ones
693 vpbroadcastq $red_tbl_idx1, $idx1
694 vpbroadcastq $red_tbl_idx2, $idx2
695 leaq `(1<<5)*2*32*8`($red_tbl), %rax # holds end of the tbl
696
697 # zeroing t0..n, cur_idx
698 vpxor $t0xmm, $t0xmm, $t0xmm
699 vmovdqa64 $t0, $cur_idx
700 ___
701 foreach (1..15) {
702 $code.="vmovdqa64 $t0, $t[$_] \n";
703 }
704 $code.=<<___;
705
706 .align 32
707 .Lloop:
708 vpcmpq \$0, $cur_idx, $idx1, %k1 # mask of (idx1 == cur_idx)
709 vpcmpq \$0, $cur_idx, $idx2, %k2 # mask of (idx2 == cur_idx)
710 ___
711 foreach (0..15) {
712 my $mask = $_<8?"%k1":"%k2";
713 $code.=<<___;
714 vmovdqu64 `${_}*32`($red_tbl), $tmp # load data from red_tbl
715 vpblendmq $tmp, $t[$_], ${t[$_]}{$mask} # extract data when mask is not zero
716 ___
717 }
718 $code.=<<___;
719 vpaddq $ones, $cur_idx, $cur_idx # increment cur_idx
720 addq \$`2*32*8`, $red_tbl
721 cmpq $red_tbl, %rax
722 jne .Lloop
723 ___
724 # store t0..n
725 foreach (0..15) {
726 $code.="vmovdqu64 $t[$_], `${_}*32`($out) \n";
727 }
728 $code.=<<___;
729
730 ret
731 .cfi_endproc
732 .size ossl_extract_multiplier_2x30_win5, .-ossl_extract_multiplier_2x30_win5
733 ___
734 $code.=<<___;
735 .section .rodata align=32
736 .align 32
737 .Lones:
738 .quad 1,1,1,1
739 .Lzeros:
740 .quad 0,0,0,0
741 ___
742 }
743
744 if ($win64) {
745 $rec="%rcx";
746 $frame="%rdx";
747 $context="%r8";
748 $disp="%r9";
749
750 $code.=<<___;
751 .extern __imp_RtlVirtualUnwind
752 .type rsaz_avx_handler,\@abi-omnipotent
753 .align 16
754 rsaz_avx_handler:
755 push %rsi
756 push %rdi
757 push %rbx
758 push %rbp
759 push %r12
760 push %r13
761 push %r14
762 push %r15
763 pushfq
764 sub \$64,%rsp
765
766 mov 120($context),%rax # pull context->Rax
767 mov 248($context),%rbx # pull context->Rip
768
769 mov 8($disp),%rsi # disp->ImageBase
770 mov 56($disp),%r11 # disp->HandlerData
771
772 mov 0(%r11),%r10d # HandlerData[0]
773 lea (%rsi,%r10),%r10 # prologue label
774 cmp %r10,%rbx # context->Rip<.Lprologue
775 jb .Lcommon_seh_tail
776
777 mov 4(%r11),%r10d # HandlerData[1]
778 lea (%rsi,%r10),%r10 # epilogue label
779 cmp %r10,%rbx # context->Rip>=.Lepilogue
780 jae .Lcommon_seh_tail
781
782 mov 152($context),%rax # pull context->Rsp
783
784 lea (%rax),%rsi # %xmm save area
785 lea 512($context),%rdi # & context.Xmm6
786 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
787 .long 0xa548f3fc # cld; rep movsq
788
789 lea `48+168`(%rax),%rax
790
791 mov -8(%rax),%rbx
792 mov -16(%rax),%rbp
793 mov -24(%rax),%r12
794 mov -32(%rax),%r13
795 mov -40(%rax),%r14
796 mov -48(%rax),%r15
797 mov %rbx,144($context) # restore context->Rbx
798 mov %rbp,160($context) # restore context->Rbp
799 mov %r12,216($context) # restore context->R12
800 mov %r13,224($context) # restore context->R13
801 mov %r14,232($context) # restore context->R14
802 mov %r15,240($context) # restore context->R14
803
804 .Lcommon_seh_tail:
805 mov 8(%rax),%rdi
806 mov 16(%rax),%rsi
807 mov %rax,152($context) # restore context->Rsp
808 mov %rsi,168($context) # restore context->Rsi
809 mov %rdi,176($context) # restore context->Rdi
810
811 mov 40($disp),%rdi # disp->ContextRecord
812 mov $context,%rsi # context
813 mov \$154,%ecx # sizeof(CONTEXT)
814 .long 0xa548f3fc # cld; rep movsq
815
816 mov $disp,%rsi
817 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
818 mov 8(%rsi),%rdx # arg2, disp->ImageBase
819 mov 0(%rsi),%r8 # arg3, disp->ControlPc
820 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
821 mov 40(%rsi),%r10 # disp->ContextRecord
822 lea 56(%rsi),%r11 # &disp->HandlerData
823 lea 24(%rsi),%r12 # &disp->EstablisherFrame
824 mov %r10,32(%rsp) # arg5
825 mov %r11,40(%rsp) # arg6
826 mov %r12,48(%rsp) # arg7
827 mov %rcx,56(%rsp) # arg8, (NULL)
828 call *__imp_RtlVirtualUnwind(%rip)
829
830 mov \$1,%eax # ExceptionContinueSearch
831 add \$64,%rsp
832 popfq
833 pop %r15
834 pop %r14
835 pop %r13
836 pop %r12
837 pop %rbp
838 pop %rbx
839 pop %rdi
840 pop %rsi
841 ret
842 .size rsaz_avx_handler,.-rsaz_avx_handler
843
844 .section .pdata
845 .align 4
846 .rva .LSEH_begin_ossl_rsaz_amm52x30_x1_ifma256
847 .rva .LSEH_end_ossl_rsaz_amm52x30_x1_ifma256
848 .rva .LSEH_info_ossl_rsaz_amm52x30_x1_ifma256
849
850 .rva .LSEH_begin_ossl_rsaz_amm52x30_x2_ifma256
851 .rva .LSEH_end_ossl_rsaz_amm52x30_x2_ifma256
852 .rva .LSEH_info_ossl_rsaz_amm52x30_x2_ifma256
853
854 .section .xdata
855 .align 8
856 .LSEH_info_ossl_rsaz_amm52x30_x1_ifma256:
857 .byte 9,0,0,0
858 .rva rsaz_avx_handler
859 .rva .Lossl_rsaz_amm52x30_x1_ifma256_body,.Lossl_rsaz_amm52x30_x1_ifma256_epilogue
860 .LSEH_info_ossl_rsaz_amm52x30_x2_ifma256:
861 .byte 9,0,0,0
862 .rva rsaz_avx_handler
863 .rva .Lossl_rsaz_amm52x30_x2_ifma256_body,.Lossl_rsaz_amm52x30_x2_ifma256_epilogue
864 ___
865 }
866 }}} else {{{ # fallback for old assembler
867 $code.=<<___;
868 .text
869
870 .globl ossl_rsaz_amm52x30_x1_ifma256
871 .globl ossl_rsaz_amm52x30_x2_ifma256
872 .globl ossl_extract_multiplier_2x30_win5
873 .type ossl_rsaz_amm52x30_x1_ifma256,\@abi-omnipotent
874 ossl_rsaz_amm52x30_x1_ifma256:
875 ossl_rsaz_amm52x30_x2_ifma256:
876 ossl_extract_multiplier_2x30_win5:
877 .byte 0x0f,0x0b # ud2
878 ret
879 .size ossl_rsaz_amm52x30_x1_ifma256, .-ossl_rsaz_amm52x30_x1_ifma256
880 ___
881 }}}
882
883 $code =~ s/\`([^\`]*)\`/eval $1/gem;
884 print $code;
885 close STDOUT or die "error closing STDOUT: $!";