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