Debian Codegraph @

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